Author : HASSAN MD TAREQ | Updated : 2021/09/09
Declaring variable
- In PowerShell, variables begins with a dollar sign (
$
) - Default value of a variable is
$null
- Usage: variable name with
$
->$FullName = $FirstName + $LastName
$FooBarBaz = "foo-bar-baz"
$Path = "C:\Windows\System32"
$Today = (Get-Date).DateTime
Details: https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_variables
Data type of a variable
- PowerShell variables are loosely typed, which means that they aren’t limited to a particular type of object
- A single variable can even contain a collection, or array, of different types of objects at the same time
Delete variable
- To delete the value of a variable:
Clear-Variable -Name MyVariable
(variable name without$
)- or
$MyVariable = $null
- To delete the variable
Remove-Variable -Name MyVariable
Remove-Item -Path Variable:\MyVariable
Variable scope
https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_scopes