|
Guy recommends :
Find out which of your VMs are a waste of space and which VMs need more resources.
|
Windows PowerShell VariablesIntroduction to Windows PowerShell VariablesAll scripting languages use placeholders or variables to hold data. Furthermore, each language has its own rules and symbols. I have found that using any PowerShell variable is straightforward, just remember to introduce your PowerShell variable with a dollar sign, for example: $memory. Topics for PowerShell's Variables
♣ PowerShell's $Dollar VariablesVariables are handy for storing data which can be used later in the script. For example, storing a file path. Creating 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. # PowerShell $ Variable Example 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 # Declaring PowerShell Integer Variable
# Declaring PowerShell Integer Variable # PS> Error "Cannot Convert value. Note 1: I cannot resist pointing out the significance of [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 # PS> Twenty 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 does not give me a 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. Reserved Words - Not To Be Used For Variables Avoid using these reserved keywords for your variables: Break, continue, do, else, elseif, for, foreach, function, filter, in, if, return, switch, until, where and while. 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: A Free Trial of the Network Performance Monitor
(NPM)
|
||||||||||||||||||||||||||||||||||||||||||||||
| 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 |
| $Env:Path | Environmental Path to files. |
| $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 |
For Example you could modify the Environment Path value thus:
®
$Env:Path = $Env:Path + ";C:\Wizzo\Stuff"
Note 6: The plus (+) means that you keep the existing path locations and append C:\Wizzo\Stuff
Note 7: Talking of Env variables, you can list them with gci thus:
Get-ChildItem Env:\
PowerShell variables are mapped to classes in the Microsoft .NET Framework. One benefit is that variables are objects and thus can be manipulated in many ways. There is also a family of variable cmdlets which you can see with this command:
Get-Command -noun variable
Although there is cmdlet called New-Variable, I have never seen anyone use it for real because you can just wade in with a declaration.
$Files = "C:\Windows" # Is so much easier than New-Variable
Get-ChildItem $Files
Declaring Variables with New-Variable
Remove-Variable $Files
New-Variable files -value "C:\windows"
Get-ChildItem $Files
PowerShell takes care of strings and numbers automatically, thus you can produce numerous excellent scripts without worrying about this PowerShell theory.
$Files = "C:\Windows" | Get-Member
Result (At the very top of the list)
TypeName
System.String
PowerShell takes care of numbers automatically, but assigns the variable to a different class.
$Bit = 64 | Get-Member
Result (At the very top of the list)
TypeName
System.Int32
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 $_.
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.
If you like this page then please share it with your friends
• PowerShell Home • Foreach loops • PowerShell Foreach • Foreach-Object cmdlet
• Syntax • Variables • -whatIf • -ErrorAction • Windows PowerShell • PowerShell 2.0
• PowerShell Functions • [System.Math] • Get-Credential • Windows 7 PowerShell 2.0
Please email me if you have a better example script. Also please report any factual mistakes, grammatical errors or broken links, I will be happy to correct the fault.

You get 36 topics organized into these 3 sections:
1) Getting Started
2) Real-life tasks
3) Examples of Syntax.
In addition to the ebook, you get a PDF version of this Introduction to PowerShell ebook It runs to 120 pages of A4.
*
Custom Search
|
Guy Recommends: WMI Monitor and It's Free!
|
|
Home Copyright © 1999-2012 Computer Performance LTD All rights reserved Please report a broken link, or an error. | |