About

Description of Windows PowerShell

Windows PowerShell is a command line shell and scripting language for Windows that supersedes the CMD.exe shell and batch language. PowerShell is also an automation engine for Windows that provides functionality similar to Windows Script Host and VBScript. The PowerShell engine is hostable allowing PowerShell functionality to be accessed from within custom applications including other PowerShell hosts such as PowerShell ISE, PowerGUI and PowerShellPlus.

PowerShell is built on top of the Microsoft .NET Framework and exposes much of the capabilities of the .NET Framework through the PowerShell scripting language. And since .NET allows for interop with COM, you can also script COM objects.

PowerShell 2.0 is integrated with Windows 7 and Windows Server 2008 R2, and is available for Windows XP SP3, Windows Server 2003 SP2 and Windows Vista SP1 and SP2.

Example Usage

# List all processes using > 100 MB of PagedMemory in descending sort order    
C:\PS> Get-Process | Where {$_.PagedMemorySize -gt 100MB} | Sort -Desc

# PowerShell can also evaluate expressions
C:\PS> "Hello World!"
Hello World!

C:\PS> (98.6 - 32) * 5/9
37

# Production orientation allows experimenation and confirmation
C:\PS> Get-ChildItem C:\Users\John *.bak -r | 
           Where {$_.LastWriteTime -gt (Get-Date).AddDays(-7)} |
           Remove-Item -WhatIf
What if: Performing operation "Remove File" on Target "C:\Users\John\foo.bak"

C:\PS> Get-Process iexp* | Stop-Process -Confirm

Confirm
Are you sure you want to perform this action?
Performing operation "Stop-Process" on Target "iexplore (7116)".
[Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is Y):

Common Gotchas

Executing EXEs via a path with spaces requires quoting the path and the use of the call operator - &

C:\PS> & 'C:\Program Files\Windows NT\Accessories\wordpad.exe'

Calling PowerShell functions does not require parenthesis or comma separated args. PowerShell functions should be called just like a cmdlet. The following examples demonstrates the problem caused by this issue e.g.:

C:\PS> function Greet($fname, $lname) {"My name is '$lname', '$fname' '$lname'"}
C:\PS> Greet('James','Bond') # Wrong way to invoke this function!!
My name is '', 'James Bond' ''

Note that both 'James' and 'Bond' are packaged up as a single argument (an array) that is passed to the first parameter. The correct invocation is:

C:\PS> Greet James Bond
My name is 'Bond', 'James' 'Bond'

Note that in PowerShell 2.0, the use of Set-StrictMode -version 2.0 will catch this type of problem.

Resources

history | show excerpt | excerpt history