PowerShell variables store values for flexible use in commands. Substitution, concatenation, and inline usage of a variable in command help dynamic command creation.
In this article, we will discuss how to use variables in command in several ways.
Use Variable in Command
To use a variable in a cmdlet, use it within the Get-Content cmdlet string:
$filePath = Get-ChildItem -Path 'D:\PS\ad.txt' Get-Content $filePath
In the above PowerShell script, the variable $filePath
stores the file location retrieved using the Get-ChildItem cmdlet. We have directly referenced a variable $filePath
in the command Get-Content by using it within a command string.
The output of the above PowerShell script for inline variable usage is:
PS C:\> $filePath = Get-ChildItem -Path 'D:\PS\ad.txt' PS C:\> Get-Content $filePath
This is sample file.
PS C:\>
Using Variable Substitution
Use variable substitution technique to use a variable in Write-Host cmdlet:
$userName = "Shell Admin" Write-Host "Hello, $userName"
In the above PowerShell script, the variable $username
is used as a variable substitution within the Write-Host cmdlet string.
The output of the above script prints the message as:
Hello, Shell Admin
PowerShell Use Variable in Command Path
You can concatenate variables with either string or variable to use it within the Get-Content cmdlet:
$folderPath = 'D:\PS' $fileName = "ad.txt" $filePath = $folderPath + "\" + $fileName Get-Content $filePath
In the above PowerShell script, the $folderPath
and $fileName
variables store the directory location and file name respectively. We have concatenated them into $filePath and used a variable within the Get-Content cmdlet to read the content of the file.
The output of the above script reads the content of the file.
Use PowerShell Variable in CMD
You can use the PowerShell variable in the cmd command using cmd.exe
:
$folderPath = "D:\PS" cmd.exe /c "dir $folderPath"
In the above PowerShell script, the variable $folderpath
contains the directory location. It is used in the cmd command using cmd.exe to get files and folders available in the specified path.
The output of the above script is:
PS C:\> $folderPath = "D:\PS"
PS C:\> cmd.exe /c "dir $folderPath"
Volume in drive D is Local Disk
Volume Serial Number is CA4B-D81A
Directory of D:\PS
07-04-2023 12:10 <DIR> .
07-04-2023 12:10 <DIR> ..
16-07-2022 22:08 <DIR> AD
07-04-2023 12:10 0 ad.txt
26-02-2023 16:10 87 comp.txt
15-08-2022 20:06 <DIR> Config
PowerShell Use Variable in Command line
You can use the variable in the cmdlet:
$processName = "notepad" Start-Process $processName
In the above PowerShell script, the $processName
variables hold the name of the process and it is used in the command line Start-Process to start the process.
The output of the above script opens the notepad.
Use Environment Variable in Command
The PowerShell environment variable provides access to the system settings and user profile. You can use the environment variable in the Write-Host cmdlet to access the variable value and use it.
Write-Host "Hello, $env:USERNAME!"
In the above PowerShell script, the $env:USERNAME
is an environment variable to get the current user name and it is used in the command to print the user name to the console.
Use Variable in Invoke-Command
The Invoke-Command cmdlet is used to run the script block on the remote computer. You can use a variable in the invoke-command cmdlet:
$serverName = "EU-IT-101" $scriptBlock = {Get-ChildItem C:\Temp} Invoke-Command -ComputerName $serverName -ScriptBlock $scriptBlock
In the above PowerShell script, the $serverName
variable holds the remote computer name. The invoke-command
uses the -ComputerName
parameter to run the script block on the remote computer specified by the variable name $serverName
.
The output of the above script when run, will run the “Get-ChildItem” cmdlet on the remote computer to retrieve items from the specified path on the remote computer and returns the results to the local computer.
Conclusion
I hope the above article on how to use the PowerShell variable in command is helpful to you.
Using the variables in the PowerShell command allows you to create dynamic scripts.
You can find more topics about PowerShell Active Directory commands and PowerShell basics on the ShellGeek home page.