Getting started with PowerShellLoopsOperatorsUsing ShouldProcessPowerShell ClassesSwitch statementWorking with ObjectsUsing existing static classesBasic Set OperationsPowerShell FunctionsSending EmailHandling Secrets and CredentialsPowershell RemotingPowerShell "Streams"; Debug, Verbose, Warning, Error, Output and InformationVariables in PowerShellCommunicating with RESTful APIsWorking with the PowerShell pipelinePowerShell Background JobsReturn behavior in PowerShellWorking with XML FilesIntroduction to PsakeUsing the progress barStringsTCP Communication with PowerShellSharePoint ModuleAliasesAutomatic VariablesEnvironment VariablesPowershell profilesEnforcing script prerequisitesUsing the Help SystemSplattingDesired State ConfigurationSigning ScriptsSecurity and CryptographyCSV parsingIntroduction to PesterModules, Scripts and FunctionsPowerShell.exe Command-LineCommon parametersParameter setsRegular ExpressionsPowerShell Dynamic ParametersWMI and CIMGUI in PowershellConditional logicURL Encode/DecodeMongoDBRunning ExecutablesError handlingHashTablesActiveDirectory modulepowershell sql queriesAutomatic Variables - part 2Package managementCmdlet NamingBuilt-in variablesCreating DSC Class-Based ResourcesPowershell ModulesPowerShell WorkflowsHow to download latest artifact from Artifactory using Powershell script (v2.0 or below)?Calculated PropertiesSpecial OperatorsAnonymize IP (v4 and v6) in text file with PowershellComment-based helpAmazon Web Services (AWS) Simple Storage Service (S3)Amazon Web Services (AWS) RekognitionPSScriptAnalyzer - PowerShell Script AnalyzerNaming ConventionsEmbedding Managed Code (C# | VB)Archive ModuleInfrastructure AutomationScheduled tasks moduleISE module

Variables in PowerShell

Other topics

Simple variable

All variables in powershell begin with a US dollar sign ($). The simplest example of this is:

$foo = "bar"

This statement allocates a variable called foo with a string value of "bar".

Removing a variable

To remove a variable from memory, one can use the Remove-Item cmdlet. Note: The variable name does NOT include the $.

Remove-Item Variable:\foo

Variable has a provider to allow most *-item cmdlets to work much like file systems.

Another method to remove variable is to use Remove-Variable cmdlet and its alias rv

$var = "Some Variable" #Define variable 'var' containing the string 'Some Variable'
$var #For test and show string 'Some Variable' on the console

Remove-Variable -Name var
$var 

#also can use alias 'rv'
rv var

Scope

The default scope for a variable is the enclosing container. If outside a script, or other container then the scope is Global. To specify a scope, it is prefixed to the variable name $scope:varname like so:

$foo = "Global Scope"
function myFunc {
    $foo = "Function (local) scope"
    Write-Host $global:foo
    Write-Host $local:foo
    Write-Host $foo
}
myFunc
Write-Host $local:foo
Write-Host $foo

Output:

    Global Scope
    Function (local) scope
    Function (local) scope
    Global Scope
    Global Scope

Reading a CmdLet Output

By Default, powershell would return the output to the calling Entity. Consider Below Example,

Get-Process -Name excel   

This would simply, return the running process which matches the name excel, to the calling entity. In this case, the PowerShell Host. It prints something like,

Handles  NPM(K)    PM(K)      WS(K) VM(M)   CPU(s)     Id  SI ProcessName                                                                                                                     
-------  ------    -----      ----- -----   ------     --  -- -----------                                                                                                                     
   1037      54    67632      62544   617     5.23   4544   1 EXCEL 

Now if you assign the output to a variable, it simply wont print anything. And of course the variable holds the output. (Be it a string, Object - Any type for that matter)

$allExcel = Get-Process -Name excel

So, lets say you have a scenario where you want to assign a variable by a Dynamic name, you can use the -OutVariable parameter

Get-Process -Name excel -OutVariable AllRunningExcel

Note that the '$' is missing here. A major difference between these two assignments is that, it also prints the output apart from assigning it to the variable AllRunningExcel. You can also choose to assign it to an another variable.

$VarOne = Get-Process -Name excel -OutVariable VarTwo

Albeit, the above scenario is very rare, both variables $VarOne & $VarTwo will have the same value.

Now consider this,

Get-Process -Name EXCEL -OutVariable MSOFFICE
Get-Process -Name WINWORD -OutVariable +MSOFFICE

The first statement would simply get excel process & assign it to MSOFFICE variable, and next would get ms word processes running and "Append" it to the existing value of MSOFFICE. It would look something like this,

Handles  NPM(K)    PM(K)      WS(K) VM(M)   CPU(s)     Id  SI ProcessName                                                                                                                     
-------  ------    -----      ----- -----   ------     --  -- -----------                                                                                                                     
   1047      54    67720      64448   618     5.70   4544   1 EXCEL                                                                                                                           
   1172      70    50052      81780   584     1.83  14968   1 WINWORD     

List Assignment of Multiple Variables

Powershell allows multiple assignment of variables and treats almost everything like an array or list. This means that instead of doing something like this:

$input = "foo.bar.baz"
$parts = $input.Split(".")
$foo = $parts[0]
$bar = $parts[1]
$baz = $parts[2]

You can simply do this:

$foo, $bar, $baz = $input.Split(".")

Since Powershell treats assignments in this manner like lists, if there are more values in the list than items in your list of variables to assign them to, the last variable becomes an array of the remaining values. This means you can also do things like this:

$foo, $leftover = $input.Split(".") #Sets $foo = "foo", $leftover = ["bar","baz"]
$bar = $leftover[0] # $bar = "bar"
$baz = $leftover[1] # $baz = "baz"

Arrays

Array declaration in Powershell is almost the same as instantiating any other variable, i.e. you use a $name = syntax. The items in the array are declared by separating them by commas(,):

$myArrayOfInts = 1,2,3,4
$myArrayOfStrings = "1","2","3","4"

Adding to an arry

Adding to an array is as simple as using the + operator:

$myArrayOfInts = $myArrayOfInts + 5
//now contains 1,2,3,4 & 5!

Combining arrays together

Again this is as simple as using the + operator

$myArrayOfInts = 1,2,3,4
$myOtherArrayOfInts = 5,6,7
$myArrayOfInts = $myArrayOfInts + $myOtherArrayOfInts
//now 1,2,3,4,5,6,7

Contributors

Topic Id: 3457

Example Ids: 11952,11953,11954,18112,19980,26232

This site is not affiliated with any of the contributors.