A variable is a unit of memory in which values are stored. In Windows PowerShell, variables are represented by text strings that start with a dollar sign ($) and can contain alphanumeric characters and underscore in their names, such as $a, $location, or $my_var.
You can store all types of values in PowerShell variables.
They are typically used to store the results of commands and to store elements that are used in commands and expressions, such as names, paths, settings, and values.
You don’t need to declare the variable before using it, just use an assignment statement to assign a value to the variable.
PS> $location = Get-Location PS> $processes = Get-Process PS> $Today = (Get-Date).date
To display the value of a variable, type the variable name, preceded by a dollar sign ($).
PS> $location PS> $Today
To get information of variable, Get-Member cmdlet can tell the type of variable being used.
PS> $location | Get-Member
To change the value of a variable, assign a new value to the variable.
To delete the value of a variable, use the Clear-Variable cmdlet or change the value to $null.
PS> Clear-Variable -name location -or- PS> $location = $null
To delete the variable, use the Remove-Variable or Remove-Item cmdlets.
PS> remove-variable -name location PS> remove-item -path variable:location
To get a list of all of the variables in your PowerShell session, type:
$HOME: Represents the full path of the user’s home directory.
$HOST: Represents an object that represents the current host application for PowerShell.
$PID: Represents the process identifier (PID) of the process that is hosting the current PowerShell session.
$PROFILE: Represents the full path of the PowerShell profile for the current user and the current host application.
$NULL: $null is an automatic variable that contains a NULL or empty value. You can use this variable to represent an absent or undefined value in commands and scripts.
$TRUE: Represents TRUE. You can use this variable to represent TRUE in commands and scripts.
$FALSE: Represents FALSE. You can use this variable to represent FALSE in commands and scripts instead of using the string “false”.
$ERROR: Represents an array of error objects that represent the most recent errors.
$ARGS: Represents an array of the undeclared parameters and/or parameter values that are passed to a function, script, or script block.
$$ Represents the last token in the last line received by the session.
$? Represents the execution status of the last operation. It contains TRUE if the last operation succeeded and FALSE if it failed.
$^ Represents the first token in the last line received by the session.
$_ Contains the current object in the pipeline object. You can use this variable in commands that perform an action on every object or on selected objects in a pipeline.
$EVENT: Represents a PSEventArgs object that represents the event that is being processed.
$EVENTARGS: Represents an object that represents the first event argument that derives from EventArgs of the event that is being processed.