Variables
$ is used in PowerShell to create and use variables.
First time in
Programming, you can use space in variable.
You can use latters, numbers, spaces and underscores in
variables.
Example:
$MyVar = 7
${My Var} = “Test”
You can also give Variable type like string, integer.
Example:
[String]$MyName
= “Ronak”
[int]$Test
= 6
Now if you give $Test = “test” it will give error. Because
$Test is fixed for type [int].
You can use variable as multiple types by using runtime type
casting.
Example:
$x =
[int] 6 ß
This will store 6 as integer in variable x
Now, $x = “Test” ß
This will store string into variable x.
You can use all variable types which are available in .NET.
You can also use following commands:
- New-Variable
- Set-Variable
- Get-Variable
- Clear-Variable
- Remove-Variable
Quotation Marks
Pay attention! Single quotes and Double quotes performs
differently in PowerShell.
Double Quotes resolves all variables.
Single Quotes prevents substitutions.
Example:
$x = 2
"The Value of $x is $x" ß The Value of 2 is 2
'The Value of $x is $x' ß The Value of $x is $x
"The Value of `$x is $x" ß The Value of $x is 2
Back-trick (` sign available below Esc key) is used to prevent
individual substitutions.
0 comments:
Post a Comment