Strings and Arrays in PowerShell Core for Linux

45 minutes
  • 3 Learning Objectives

About this Hands-on Lab

The string data type is probably the most used data type in PowerShell. From displaying messages, prompting for input, or sending data to files, it is almost impossible to write scripts without strings being involved.

An array is a data structure designed to store a collection of items. The items can be the same type or different types.

This hands-on lab will demonstrate the use of both strings and arrays in PowerShell for Linux.

Learning Objectives

Successfully complete this lab by achieving the following learning objectives:

Perform a System Update, Register the MS RedHat Repository, and Install PowerShell
  1. Use the yum command to sync the package index files from their sources via the Internet.

    sudo yum check-update
  2. Use the yum command to install the newest versions of all installed packages on CentOS.

    sudo yum update
  3. Register the Microsoft RedHat repository.

    curl | sudo tee /etc/yum.repos.d/microsoft.repo
  4. Install PowerShell.

    sudo yum install -y powershell
  5. Start PowerShell.

Work with Strings in PowerShell
  1. Create a single quote string ‘Hello PowerShell – Today is $(Get-Date)’.

    'Hello PowerShell - Today is $(Get-Date)'
  2. Create a double quote string "Hello PowerShell – Today is $(Get-Date)".

    "Hello PowerShell - Today is $(Get-Date)"
  3. Show the properties of the double quote string using Get-Member and a pipeline.

    "Hello PowerShell - Today is $(Get-Date)" | Get-Member
  4. Create four variables (domain, firstname, lastname, and department) using the following information:

    • Domain –
    • First name – John
    • Last name – Smith
    • Department – History
      $domain = ''
      $firstname = 'John'
      $lastname = 'Smith'
      $department = 'History'
  5. Using the previously entered variables, derive the following values using the string concatenation operator (+):

    • Name = firstname lastname
    • DisplayName = firstname lastname (department)
    • SamAccountName = firstname.lastname
    • EmailAddress =
      $firstname + ' ' + $lastname
      $firstname + ' ' + $lastname + ' (' + $department + ')'
      $firstname + '.' + $lastname
      $firstname + '.' + $lastname + '@' + $domain
  6. Once again using the previously entered variables, obtain the same values using PowerShell string expansion.

    "$firstname $lastname"
    "$firstname $lastname ($department)"
Work with Arrays in PowerShell
  1. Create an array named A that contains the seven numeric (int) values of 29, 51, 6, 8, 19, 39, and 180.

    $A = 29,51,6,8,19,39,180
  2. Create an array named B using the range operator (..) containing the values 1 through 8.

    $B = 1..8
  3. Create a strongly-typed 32-bit integer array named ia containing four integers (1148, 2195, 3376, and 9000).

    [int32[]]$ia = 1148,2195,3376,9000
  4. Display all the elements in the array $A.

  5. Display the first element in the array $A.

  6. Dispay the third element in the array $A.

  7. Recreate the array $A to contain the numbers 0 through 9 using the range operator (..).

    $A = 0 .. 9
  8. Display the last three elements of the array, using negative indexes.

  9. Exit PowerShell.


Additional Resources

In order to get used to using PowerShell strings and arrays, we will open up a terminal session and practice using string creation and concatenation, as well as creating and initializing arrays.

What are Hands-on Labs

Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.

Sign In
Welcome Back!

Psst…this one if you’ve been moved to ACG!

Get Started
Who’s going to be learning?