Windows PowerShell VariablesIntroduction to Windows PowerShell VariablesAll scripting languages use placeholders or variables to hold data. Moreover, each language has its own rules and symbols. I have found that using any PowerShell variable is straightforward, just remember that PowerShell introduces variables with a dollar sign, for example: $memory. What impresses programmers is the ability to assign not just text to the variable, but also to assign an object to a variable. While most proper scripting languages are able to handle objects through variables, CMD lacks this ability. Topics for PowerShell's Variables
♣ PowerShell's $Dollar variablesCreating a PowerShell variable could not be more straightforward; just put the dollar sign in front of the name you wish to call the variable. Let us create, then set, a variable called $Mem: $Mem= WmiObject Win32_ComputerSystem Once we have created $Mem, then we can put the variable to work and calculate the RAM memory in Mega bytes. $Mem= WmiObject Win32_ComputerSystem PowerShell has no built-in mechanism for enforcing variable types, for example, string variables can begin with letters or numbers. Yet numeric variables can also begin with letters (or numbers). However, you can restrict the values a PowerShell variable accepts by preceding it with [int] or [string], for example: Example declaring the PowerShell variable as an integer [int]$a =7 $a +3 $a ="Twenty" Note: I cannot resist pointing out the [Square brackets]. The reason is that PowerShell only ever uses square brackets for optional items, and declaring the type of a variable is just that - optional. Example without declaring the variable type. $b = 7 No error here because $b was not declared as number or a string. Do you think that PowerShell variables are case sensitive or insensitive? The answer is insensitive, just as with most other commands, upper or lower case work equally. When PowerShell evaluates a potential variable name, it carries on from the $Dollar until it meets a word breaking character such as a space or punctuation mark. This gives me no problem because I only use snappy OneWord names, but if you use variables with strange characters - watch out! If you insist on using variables with names such as a*?,v**, then you could enclose them in braces - thus {a*?,v**}. Clever stuff, but best to keep it simple and don't ask for trouble I say. Incidentally, you can join string variables simply by using a plus (+) sign. The reason that I mention this is because I spent ages searching fruitlessly for a special text concatenator, only to discover that the plain plus sign was all I needed.
Guy Recommends: SolarWinds Engineer's Toolset v10
|
||||||||||||||||||||||||||||||||||||||||||||||
| Variable Name | Description |
| $_ | The current pipeline object; used in script blocks, filters, the process clause of functions, where-object, foreach-object and switch |
| $^ | contains the first token of the last line input into the shell |
| $$ | contains the last token of last line input into the shell |
| $? | Contains the success/fail status of the last statement |
| $Args | Used in creating functions that require parameters |
| $Error | If an error occurred, the object is saved in the $error PowerShell variable |
| $foreach | Refers to the enumerator in a foreach loop. |
| $HOME | The user's home directory; set to %HOMEDRIVE%\%HOMEPATH% |
| $Input | Input piped to a function or code block |
| $Match | A hash table consisting of items found by the –match operator. |
| $MyInvocation | Information about the currently script or command-line |
| $Host | Information about the currently executing host |
| $LastExitCode | The exit code of the last native application to run |
| $true | Boolean TRUE |
| $false | Boolean FALSE |
| $null | A null object |
| $OFS |
Output Field Separator, used when converting an array to a string. By default, this is set to the space character. |
| $ShellID | The identifier for the shell. This value is used by the shell to determine the ExecutionPolicy and what profiles are run at startup. |
| $StackTrace | contains detailed stack trace information about the last error |
In PowerShell, variables are easy to create, just precede the name with a dollar sign, for example $Disk. For more ambitious scripting you can restrict their type for example [int]$Memory, you can also prescribe the variable's scope, local or global.
One variable worth mastering is the special pipeline variable controlled by $_.
• PowerShell Home • Com • Shell Application • Active Directory • QAD Snap-in • Get-Member
Please write in if you see errors of any kind. Please report any factual mistakes, grammatical errors or broken links, I will be happy to not only to correct the fault, but also to give you credit.
Download my ebook:
|
*
|
|
|
|
Home Copyright © 1999-2010 Computer Performance LTD All rights reserved Please report a broken link, or an error. | |