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
- Use the
yum
command to sync the package index files from their sources via the Internet:sudo yum check-update
- Use the
yum
command to install the newest versions of all installed packages on CentOS:sudo yum update
- Register the Microsoft RedHat Repository:
curl https://packages.microsoft.com/config/rhel/7/prod.repo | sudo tee /etc/yum.repos.d/microsoft.repo
- Install PowerShell:
sudo yum install -y powershell
- Start PowerShell:
pwsh
- Use the
- Work with Creating, Changing, Using, and Removing Variables
- Set a variable named MyVar and enter the integers 10, 11, and 12 as values:
$MyVar = 10, 11, 12
- Set a variable named Path and enter the path "/home/cloud_user/" as the value:
$Path = "/home/cloud_user"
- Set a variable name Process and set the cmdlet Get-Process as the value:
$Processes = Get-Process
- Set a variable named Today and set the value as (Get-Date).DateTime as the value:
$Today = (Get-Date).DateTime
- Call the variable MyVar and observe the output:
$MyVar
- Call the variable Today and observe the output:
$Today
- Call the variable Path and observe the output:
$Path
- Call the variable Processes and observe the output:
$Processes
- Change the value of MyVar to 15,16,17
$MyVar = 15,16,17
- Call the variable MyVar and observe the change in the output:
$MyVar
- Change the value of MyVar to "The Blue Dog"
$MyVar = "The Blue Dog"
- Call the variable MyVar and observe the change in the output:
$MyVar
- Clear the variable MyVar using both the Clear-Variable and $null methods":
Clear-Variable -Name MyVar $MyVar = $null
- Call the variable MyVar and observe the change in the output:
$MyVar
- Remove the variable MyVar using the Remove-Variable cmdlet:
Remove-Variable -Name MyVar
- Run the cmdlet Get-Variable and verify that MyVar is not in the list of variables:
Get-Variable
- Exit PowerShell
exit
- Set a variable named MyVar and enter the integers 10, 11, and 12 as values: