Difference between revisions of "Power Shell"
Jump to navigation
Jump to search
m (→Variables: Added to variable types) |
m (→Variables: Added interpolation) |
||
Line 1: | Line 1: | ||
== | == Variables == | ||
All variable names are prefixed with a $, and are case '''insensitive''' (though there's no reason to not use CamelBack notation if that floats your boat). | |||
Apart from a few reserved words and characters there's little restriction on what can be used, though note that this flexibility can cause the occasional issue, whereby PowerShell gets confused as to where a variable name finishes. Variable names can be enclosed in <code> { } </code> in order to delimit them, eg <code>${varname}</scode>. | |||
{ | |||
PowerShell is all about manipulating objects, and its variables are all essentially the same, not being specifically defined as an object, string, integer, etc. Which is normally useful, however sometimes you need to force a variable to contain a data type. Using a prefix of [type] achieves this... | |||
<source lang="powershell"> [string]$result = $PingResult.Status </source> | <source lang="powershell"> [string]$result = $PingResult.Status </source> | ||
Line 63: | Line 44: | ||
|} | |} | ||
=== Strings === | |||
'''Concatenation +''' | |||
<source lang="powershell"> | <source lang="powershell"> | ||
$strAB = $strA + $strB | |||
</source> | </source> | ||
'''Interpolation''' | |||
Interpolation allows variables to be embedded into a string and to be resolved into their actual values. This works between double quotes, but not between single quotes... | |||
<source lang="powershell"> | <source lang="powershell"> | ||
PS | PS E:\> $sub = "replaced" | ||
PS E:\> Write-Output "Variable has been $sub" | |||
Variable has been replaced | |||
PS E:\> Write-Output 'Variable has been $sub' | |||
Variable has been $sub | |||
PS | |||
PS | |||
</source> | </source> | ||