Skip to content

Contact sales

By filling out this form and clicking submit, you acknowledge our privacy policy.
  • Labs icon Lab
  • A Cloud Guru
Google Cloud Platform icon
Labs

Strings and Arrays in PowerShell Core for Linux

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.

Google Cloud Platform icon
Labs

Path Info

Level
Clock icon Intermediate
Duration
Clock icon 45m
Published
Clock icon Apr 24, 2020

Contact sales

By filling out this form and clicking submit, you acknowledge our privacy policy.

Table of Contents

  1. Challenge

    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
    
    1. Use the yum command to install the newest versions of all installed packages on CentOS.
    sudo yum update
    
    1. Register the Microsoft RedHat repository.
    curl https://packages.microsoft.com/config/rhel/7/prod.repo | sudo tee /etc/yum.repos.d/microsoft.repo
    
    1. Install PowerShell.
    sudo yum install -y powershell
    
    1. Start PowerShell.
    pwsh
    
  2. Challenge

    Work with Strings in PowerShell

    1. Create a single quote string 'Hello PowerShell - Today is $(Get-Date)'.
    'Hello PowerShell - Today is $(Get-Date)'
    
    1. Create a double quote string "Hello PowerShell - Today is $(Get-Date)".
    "Hello PowerShell - Today is $(Get-Date)"
    
    1. Show the properties of the double quote string using Get-Member and a pipeline.
    "Hello PowerShell - Today is $(Get-Date)" | Get-Member
    
    1. 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'
    
    1. 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 = [email protected]
    $firstname + ' ' + $lastname
    $firstname + ' ' + $lastname + ' (' + $department + ')'
    $firstname + '.' + $lastname
    $firstname + '.' + $lastname + '@' + $domain
    
    1. 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"
    
  3. Challenge

    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
    
    1. Create an array named B using the range operator (..) containing the values 1 through 8.
    $B = 1..8
    
    1. Create a strongly-typed 32-bit integer array named ia containing four integers (1148, 2195, 3376, and 9000).
    [int32[]]$ia = 1148,2195,3376,9000
    
    1. Display all the elements in the array $A.
    $A
    
    1. Display the first element in the array $A.
    $A[0]
    
    1. Dispay the third element in the array $A.
    $A[2]
    
    1. Recreate the array $A to contain the numbers 0 through 9 using the range operator (..).
    $A = 0 .. 9
    
    1. Display the last three elements of the array, using negative indexes.
    $A[-1..-3]
    
    1. Exit PowerShell.
    exit
    

The Cloud Content team comprises subject matter experts hyper focused on services offered by the leading cloud vendors (AWS, GCP, and Azure), as well as cloud-related technologies such as Linux and DevOps. The team is thrilled to share their knowledge to help you build modern tech solutions from the ground up, secure and optimize your environments, and so much more!

What's a lab?

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.

Provided environment for hands-on practice

We will provide the credentials and environment necessary for you to practice right within your browser.

Guided walkthrough

Follow along with the author’s guided walkthrough and build something new in your provided environment!

Did you know?

On average, you retain 75% more of your learning if you get time for practice.

Start learning by doing today

View Plans