Working with Variables in PowerShell Core for Linux

30 minutes
  • 2 Learning Objectives

About this Hands-on Lab

You can store all types of values in PowerShell variables. For example, store the results of commands, and store elements that are used in commands and expressions, such as names, paths, settings, and values.

This lab will cover the concepts of creating and using variables 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 Creating, Changing, Using, and Removing Variables
  1. Set a variable named MyVar and enter the integers 10, 11, and 12 as values:
    $MyVar = 10, 11, 12
  2. Set a variable named Path and enter the path "/home/cloud_user/" as the value:
    $Path = "/home/cloud_user"
  3. Set a variable name Process and set the cmdlet Get-Process as the value:
    $Processes = Get-Process
  4. Set a variable named Today and set the value as (Get-Date).DateTime as the value:
    $Today = (Get-Date).DateTime
  5. Call the variable MyVar and observe the output:
  6. Call the variable Today and observe the output:
  7. Call the variable Path and observe the output:
  8. Call the variable Processes and observe the output:
  9. Change the value of MyVar to 15,16,17
    $MyVar = 15,16,17
  10. Call the variable MyVar and observe the change in the output:
  11. Change the value of MyVar to "The Blue Dog"
    $MyVar = "The Blue Dog"
  12. Call the variable MyVar and observe the change in the output:
  13. Clear the variable MyVar using both the Clear-Variable and $null methods":
    Clear-Variable -Name MyVar
    $MyVar = $null
  14. Call the variable MyVar and observe the change in the output:
  15. Remove the variable MyVar using the Remove-Variable cmdlet:
    Remove-Variable -Name MyVar
  16. Run the cmdlet Get-Variable and verify that MyVar is not in the list of variables:
  17. Exit PowerShell

Additional Resources

In order to get used to using PowerShell variables and the cmdlets related to variables, you will open up a terminal session, and practice creating, changing, and using variables.

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?