Use Variables in Windows PowerShell
Learn On Demand
Learn On Demand Pro Series

Time
45 minutes
Difficulty
Beginner

In this IT Pro Challenges virtual lab, learners are introduced to the skills required to use environment and default variables. Exercises in this lab teach users how to manipulate, assign, and calculate integer and string variables in Powershell. The topics covered in this lab are critical for learners to be effective in system administrator job ro...

Start your free 3-day trial and become one of the 3 million Cybersecurity and IT professionals advancing their career goals

Sign up with
Or

Already have an account? Sign In »

Overview

PowerShell is a task automation and configuration management tool. It contains a command-line shell and scripting language. PowerShell is constructed on top of the .NET Common Language Runtime (CLR) and receives and delivers .NET objects. This major shift produces completely new tools and techniques for automation.

In this hands-on lab, you will learn how to use variables in Windows PowerShell. First, you will create and access files using environment variables. Next, you will manipulate text using a string variable. Finally, you will assign a value to an integer variable and then perform calculations using variables.

Understand the Scenario

In this virtual lab, you are a system administrator for a company that needs to automate the management of a large number of servers by using scripts. To accomplish this task, You will use an Azure resource group that contains an Azure virtual machine named LabVM. The virtual machine configuration could take five to ten minutes and may not be complete when you begin this challenge. You will receive a notification when the automated configuration is complete. Take a moment to familiarize yourself with what you have in the environment.

Configure your environment

In this section of the lab, you will configure the environment. First, you will display the LabVM virtual machine, and then ensure that the virtual machine status is Running. Make sure that the virtual machine has finished booting up and is running before you attempt to connect. Next, you will select Connect, select Download RDP File, open the RDP file, and then connect to the virtual machine. When prompted to enter your credentials, you will sign in to the virtual machine, and in the certificate warning dialog box, select Yes. Finally, you will open Windows PowerShell as an administrator and create a folder named C:\Class and then change to that folder. If you are presented with a low memory warning after signing in to the virtual machine, dismiss it.

Display and use default variables

In this section of the lab, learners will now display and use default variables. First, in Windows PowerShell, they will run a command to retrieve all of the available variables and display the content of the variables such as $PSVersionTable, $Home, $PSHome, $True, and $False. The default variables can be used in commands and Windows PowerShell scripts to perform actions based upon their content. Some of these variables, like $FormatEnumerationLimit and ErrorActionPreference, can be modified to change the behavior of Windows PowerShell. Next, they will run a command to change to the Windows PowerShell home folder by using a variable. Finally, they will check and confirm that they retrieved all of the available variables and displayed the content of the $PSVersionTable, $Home, $PSHome, $True, and $False variables. They have changed to the Windows PowerShell home folder by using a variable.

Display and use environment variables

In this section of the lab, learners will now display and use environment variables. First, they will run a command to list all of the environment variables to display the content of the variables such as $env:SystemDrive, $env:Path, $env:UserName, and $env:ComputerName. Both Windows PowerShell and a Windows command prompt use the same environment variable names; however, in Windows PowerShell, you access the variables by using $ENV: before the names. Next, they will change to the System drive by using a variable and then retrieving all processes from the local machine using the ComputerName environment variable. Finally, in Windows PowerShell, they will run a command to open the new file in Notepad and then check and confirm that they ran a command to retrieve all processes from the local computer, and then output the list to a file by using a file path and name that are based on the $Home variable and the $env:ComputerName environment variable.

Manipulate text using a string variable

In this section of the lab, learners will learn how to manipulate text using the string variable. First, in Windows PowerShell, they will create a variable named VarOne that contains the text "This is Windows PowerShell, and then display the content of the variable". Next, they will display all of the available methods for the VarOne variable and use a pipeline command to convert the string in the VarOne variable to uppercase and then use a pipeline command to replace the text "This is with I am in the VarOne variable". Next, they will append the text in a challenge lab! to VarOne and then send the output to a file named C:\Class\LabString.txt and use Windows PowerShell to display the content of C:\Class\LabString.txt on the screen. Finally, they will remove the VarOne variable from memory and set the variable VarOne equal to the content of the C:\Class\LabString.txt file, and then display the variable and then run a command to output the text "My file contains: "to the screen with the variable VarOne embedded in the output. Another handy feature of PowerShell is the ability to embed a variable directly within a double-quoted string. You don't have to perform concatenation of the string and the variable like you do in most other languages.

Assign an integer value to a variable

In this section of the lab, learners will learn how to assign an integer value to a variable. First, they will create a variable named VarTwo that contains the number 200 and then set the value of VarTwo to Two Hundred. Next, they will specify that VarTwo is an integer and then set the value to 200. Next, they will set the value of VarTwo to Two Hundred again. This will generate an error. By default, Windows PowerShell is very flexible about allowing variables to change data types. However, in a script, you should assign the variable a strict data type to help with debugging later. Finally, they will display the content of VarTwo.

Perform a calculation using a variable in Windows PowerShell

In this section of the virtual lab, learners will learn how to perform a calculation using a variable in Windows Powershell. First, they will create a variable named Mem, and then, they will multiply the variable VarTwo by 5, assign the result to Mem, and then display the content of Mem. Next, they will add 1000 to the Mem variable, store the result in Mem, and display the content of Mem. Next, they will divide the Mem variable by 20, store the result in Mem, and display the content of Mem. Using the order of operations, they will subtract 90 from Mem, multiply the result by 2, and display the content of Mem. Windows PowerShell makes it easy to do simple math at the command prompt. You don't need to use a variable to store the result if all you want to do is see it. Windows PowerShell supports adding, subtracting, multiplying, and dividing at the prompt.

Lab Summary Conclusion

After completing the "Use Variables in Windows PowerShell" virtual lab, you will have accomplished the following:

  • Display and use default variables.
  • Display and use environment variables.
  • Manipulate text using a string variable.
  • Assign an integer value to a variable.
  • Perform a calculation using a variable in Windows PowerShell.