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 https://packages.microsoft.com/config/rhel/7/prod.repo | sudo tee /etc/yum.repos.d/microsoft.repo
  4. Install PowerShell.

    sudo yum install -y powershell
  5. Start PowerShell.

    pwsh
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 – scriptingguru.com
    • First name – John
    • Last name – Smith
    • Department – History
      $domain = 'scriptingguru.com'
      $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@scriptingguru.com
      $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)"
    "$firstname.$lastname"
    "$firstname.$lastname@$domain"
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.

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

    $A[0]
  6. Dispay the third element in the array $A.

    $A[2]
  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.

    $A=[-1..-3]
  9. Exit PowerShell.

    exit

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.

Get Started
Who’s going to be learning?

How many seats do you need?

  • $499 USD per seat per year
  • Billed Annually
  • Renews in 12 months

Ready to accelerate learning?

For over 25 licenses, a member of our sales team will walk you through a custom tailored solution for your business.


$2,495.00

Checkout
Sign In
Welcome Back!

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