|
Guy recommends :
Find out which of your VMs are a waste of space and which VMs need more resources.
|
Windows PowerShell $_. VariableIntroduction to the PowerShell Dollar VariableConstructions such as ... | Where {$_.name -match "win"} are incredibly useful in PowerShell. Definitions such as: '$_ means in this pipeline', are a bit stuffy. The best way to understand $_ is to work through examples. Examples of the PowerShell $_ Variable
♣ PowerShell's $_ VariableThe first point to remember is that $_ is a variable or placeholder. # PowerShell $_ Variable Example What we want to say here is In a nutshell $_ saves us repeating Get-Service, or whatever else may be to the left of ... | Where{$_ The Significance of the Dot in PowerShell's $_.Here is a similar example but featuring .DisplayName instead of .Name. My point is to illustrate how the .dot command introduces a property. # PowerShell $_ Variable Example Challenge: Research more properties with Get-Service | Get-Member. $_ Example to Filter WmiObjectsThe key to remembering the syntax is to breakdown the construction in to: $dollar / _underscore / dot.property. The commonest example would be: $_.name. # PowerShell script to find Network WMI Objects Note: The real-life task is to research for network type WMI objects. Without the where clause it would be like looking for a needle in a haystack.
Guy
Recommends: WMI Monitor and It's Free!
|
||||||||||||||||||||||||||||||||||||
| Variable Name | Description |
| $_ | The current pipeline object; used in script blocks, filters, the process clause of functions, where-object, foreach-object and switch |
| $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. |
| $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 |
| $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 |
Perhaps the key to understanding this construction is to look at what follows $_. For example, $_.Name leads us to think, 'Name is a property in the current scriptblock, or PowerShell pipeline. Further reflection shows that $_ saves scripting space by substituting two characters for a whole extra construction.
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!
|
|
Author: Guy Thomas Copyright © 1999-2012 Computer Performance LTD All rights reserved. Please report a broken link, or an error to: | |