- Lab
- A Cloud Guru
Working with Variables in PowerShell Core for Linux
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.
Path Info
Table of Contents
-
Challenge
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
-
Challenge
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
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.