

Start with $ sign followed by a variable name and then assign a value by using the “=” operator. $mystart_date=::(-15) How to declare a PowerShell Variable $myDateStr = (get-date).ToString("yyyyMM") Need to specify the type in a bracket before the variable name. The advantage of creating a strong type variable is to prevent exceptions in later calculations. Strongly typed variableĪlthough we do not need to specify the data type for a PowerShell variable it can store any type of data but still, it is allowed if we need to create a strongly typed variable. These words have special significance in PowerShell hence cannot be used as a variable name.

The Reserved words are those words that cannot be used as a variable name like – if, else, elseif, break, continue, do, for, foreach, function, filter, in, if, return, switch, until, where, while. The default value of the preference variable can be changed by the user but these variables are maintained by PowerShell itself.Įxample – $ErrorActionPreference, $VerbosePreference, $InformationPreference, $WarningPreference, $DebugPreference, $ConfirmPreference etc. These variables are created and maintained by Windows PowerShell itself users can’t change the value of these variables.Įxample – $Error, $Args, $Event, $Foreach, $Exception etc.ģ.Preference Variables – The user preferences for the Windows PowerShell are store in the preference variable. Users can change, clear, remove these variables anytime.Ģ.Automatic Variables – Variables that store state information for PowerShell. A global variable can be declared like below-Įxample- $Global:myVariable = “This is an explicit global variable” Types of PowerShell Variablesġ.Custom Variables – These variables are created and maintained by the user itself. While a global variable has a large scope that can be accessed throughout the current PowerShell session. Local variables are those variables that have a scope within a function or a block of code where they are declared that cannot be accessed outside that scope. Like any other programming, the variable scope in PowerShell is either global or local. Therefore, PowerShell variables do not require data types and any value such as integer, string, float, object, etc. In this example, the variable “a” is the name of the variable and it stored integer as well as string value. To display the value of a variable, simply enter the variable on the console. To assign a value to a variable, the “=” operator is used. All the programming languages used variables to load some data.Ī variable in PowerShell begins with “$” (dollar symbol) and its name can contain any letters, numbers, and underscores.

Variables are the smallest unit of any program or script it saved data in memory.
