Saturday, June 7, 2008

Introduction to Powershell for Developers

From: Desert Code Camp 2007
Speaker: Anthony Park

Notes:

Basics:

{ - delineates a block of code.

$ - precedes a variable

-eq is 'equal'

-ne is 'not equal'

= is assignment

Store time intensive command results in a variable for later use.

e.g.: $b = Get-EventLog System

$_ is the self (this) variable.

Pipe variable or command results into the Get-Member command to see members of the object. eg: $b | Get-Member

To group a collection by one of the fields pipe the collection into the Group-Object and specify the member name as the following parameter. e.g. $b | Group-Object Source

You should sort the collection before grouping. e.g. $b | Sort-Object Source | Group-Object Source

You can filter a collection before (or after) applying the above command using the Where-Object. e.g. $b | Where-Object { $_.Source.StartsWith('S') } | Sort-Object Source | Group-Object Source

The $profile variable holds the default profile that gets run when you open Powershell. If you want to customize your Powershell then typing $profile in the Powershell environment will show you where your default profile is so that you can edit it.

 To retrieve a web page into a variable try: $mypage = (New-Object Net.WebClient).DownloadString('http://guyellisrocks.com')

 

 

No comments:

Post a Comment