Learning PowerShell Part 1

04/18/09

Learning PowerShell Part 1

“Learning” blog serials seem popular lately. I am a rank amateur with Windows PowerShell, but you’re welcome to stumble along with me.

References

Windows PowerShell in Action (eBook) - Bruce Payette

See also: http://www.manning.com/payette2/

Essential PowerShell (book) - Holger Schwichtenberg

Setup

Windows PowerShell download

PowerShell Community Extensions

I tried the demo of PowerShell Plus - I liked the IntelliSense and debugging, but the trial period was too short to convince me to shell (heh) out $145. A coworker (Justin) suggested the free PowerGUI; I’ve installed this but haven’t done much more.

If you want to run scripts (.ps1 files), the first thing you’ll need to do is set the security policy with Set-ExecutionPolicy. RemoteSigned is the easiest (if not the most secure) option for learning.

> Set-ExecutionPolicy RemoteSigned

Help

Use “help” or “man” (aliases for Get-PagedHelp) to get help on a command.

> help Get-Process

Spacebar goes to the next page; q quits. (UNIX users will find this more ;) familiar.)

Help with no parameter lists available aliases, cmdlets (basically commands), providers, and so on.

Get-Command shows you the available cmdlets.

Tab-completion is a handy feature; try typing “Get-” and then hitting [TAB] repeatedly. It works for parameters, too; try typing “Get-Process -” followed by several [TAB]s.

Objects

Not only does PowerShell give you the true piping power of UNIX shells, it does more. Rather than piping text, it pipes objects.

$_ represents the current object in the pipeline (in ForEach-Object, Where-Object, etc.).

Get-Member (alias gm) is your friend. You can pipe an object (or objects) into it to get information on the object.

> Get-Process | gm

This tells you that Get-Process returns a collection of System.Diagnostics.Process objects and lists the members.

Note that Select-Object (alias select) will screw with Get-Member, as it creates new PSCustomObject instances.

Aliases

While you may not want to use aliases extensively in scripts (ever tried to read Perl?), they are your friend at the command line.

Get-Alias (alias gal) is your gal-friend. (Couldn’t resist.)

Show all aliases:

> gal

Show the definition for the ps alias:

> gal ps

Show all the aliases of a cmdlet:

> gal | ? { $_.Definition -eq "Get-Process" }

(? is the alias for Where-Object.)

I/O

Common DOS and UNIX commands (cd, pwd, dir, ls, cat, etc.) are often aliased or otherwise made available to the shell.

Get-PSDrive (alias gdr) shows you the available drives. You can treat different providers in the same manner, which makes it possible to do things like getting a listing of all environment variables:

> dir env:

Note that you add a colon (:) on the end, as you would with a drive letter (C:).

One way to get the contents of a file is with variable notation and a provider:

> ${c:foo.txt} | ? {$_ -match "java.*version"} | sort

Alternately, you could use Get-Content (aliases gc, cat, type).


Your Host: webmaster@truewill.net
Copyright © 2000-2013 by William Sorensen. All rights reserved.