Home » PowerShell » PowerShell Variable Assignment

PowerShell Variable Assignment

PowerShell variable assignment is an essential concept in scripting to store and manipulate data. Variable stores the data of different types such as strings, numbers, and objects.

Use equal signs (=) to assign the value to a variable in PowerShell.

In this article, we will discuss how to assign a value to a variable in PowerShell.

Basic PowerShell Variable Assignment

In PowerShell, you can declare a variable by using the dollar ( $) character followed by the variable name, to assign a value to a variable, use the equal sign = :

$userName = "ShellAdmin"

In the above PowerShell script, the variable name is $userName which stores the value of the string type assigned to it.

The output of the above script is:

PS C:\> $userName = "ShellAdmin"                                                                                        PS C:\> $userName                                                                                                       ShellAdmin
PS C:\> 

Variable Assignment for Strongly-Typed Variables

In PowerShell, the strongly-typed variable can be created by specifying the data type before the variable name and using the equal sign = to assign the value to a variable.

[int]$bookValue = 25

The output of the above PowerShell script is:

PS C:\> [int]$bookValue = 25                                                                                            
PS C:\> $bookValue                                                                                                      
25
PS C:\>     

Type Casting – Assign Variable Value

You can also cast a variable’s type by specifying the type in the square brackets before the value.

$bookValue = [int]"25"

Use Where-Object cmdlet for Variable Assignment

The Where-Object cmdlet in PowerShell allows you to filter objects based on their properties and can help to assign a value to a variable.

$processes = Get-Process                                                                                        $peakWorkingProcesses = $processes | Where-Object {$_.PeakWorkingSet64 -gt 100MB}                               

In the above PowerShell script, the Get-Process cmdlet retrieves all the running process in the Windows OS and assign them to the variable $processes.

Later, $processes output pipe to the Where-Object cmdlet to apply a filter based on properties and retrieves all the process that matches the criteria, and assigns the value to the $peakWorkingProcesses variable.

Common Variable Assignment

You can assign the output of the command to a variable in PowerShell:

$psVersion = $HOST.Version  

In the above script, the built-in variable $Host gets the PowerShell version and assigns its output to the variable $psVersion.

You can also assign multiple values to a variable in PowerShell:

$scores = 70,75,64

Conclusion

I hope the above article on PowerShell variable assignment using the different ways is helpful to you. Understanding variable assignment is essential for creating efficient and effective scripts.

You can find more topics about PowerShell Active Directory commands and PowerShell basics on the ShellGeek home page.