PowerShell Primer

About PowerShell

Windows PowerShell® is a task-based command-line shell and scripting language designed especially for system administration. Built on the .NET Framework, Windows PowerShell helps IT professionals and power users control and automate the administration of the Windows operating system and applications that run on Windows.
http://technet.microsoft.com/en-us/library/bb978526.aspx

Download PowerShell

Windows Management Framework 4.0 includes updates to Windows PowerShell, Windows PowerShell ISE, Windows PowerShell Web Services (Management OData IIS Extension), Windows Remote Management (WinRM), Windows Management Instrumentation (WMI), the Server Manager WMI provider, and a new feature for 4.0, Windows PowerShell Desired State Configuration (DSC).
http://www.microsoft.com/en-us/download/details.aspx?id=40855

Starting PowerShell

The Windows PowerShell Integrated Scripting Environment (ISE) is a host application for Windows PowerShell. In Windows PowerShell ISE, you can run commands and write, test, and debug scripts in a single Windows-based graphic user interface with multiline editing, tab completion, syntax coloring, selective execution, context-sensitive help, and support for right-to-left languages. You can use menu items and keyboard shortcuts to perform many of the same tasks that you would perform in the Windows PowerShell console. For example, when you debug a script in the Windows PowerShell ISE, to set a line breakpoint in a script, right-click the line of code, and then click Toggle Breakpoint.
http://technet.microsoft.com/en-us/library/dd315244.aspx

Do one of the following:

    • Click Start, point to All Programs, point to Windows PowerShell V2, and then click Windows PowerShell ISE.
    • In the Windows PowerShell console Cmd.exe, or in the Run box, type, powershell_ise.exe.

Hello World!

The Write-Host cmdlet enables you to write messages to the Windows PowerShell console.
http://technet.microsoft.com/en-us/library/ee177031.aspx

  1. Open PowerShell ISE
  2. Click File and then click New
  3. Type Write-Host "Hello World!"
  4. Execute the script by pressing [F5]
"Hello World!"

Variables

When you write a script, particularly a system administration script, you rarely get to hard-code in all your values ahead of time; instead, you typically need to retrieve information, store that information in a variable or two, and then display the values of those variable.
http://technet.microsoft.com/en-us/library/ee692790.aspx

PowerShell is influenced by Unix shells and Perl so variables are prefixed with a $sigil.

$message = 'Hello World!'
Write-Host $message
"Hello World!"

Functions

A function is a list of Windows PowerShell statements that has a name that you assign. When you run a function, you type the function name. The statements in the list run as if you had typed them at the command prompt.
http://technet.microsoft.com/en-us/library/hh847829.aspx

Windows PowerShell uses a verb-noun pair for the names of cmdlets and for their derived Microsoft .NET Framework classes. For example, the Get-Command cmdlet provided by Windows PowerShell is used to retrieve all the commands that are registered in Windows PowerShell. The verb part of the name identifies the action that the cmdlet performs. The noun part of the name identifies the entity on which the action is performed.
http://msdn.microsoft.com/en-us/library/ms714428(v=vs.85).aspx

$message = "Hello World!"
function Write-Message()
{
    Write-Host $message
}
# Call the function
Write-Message
"Hello World!"

Passing Variables to Functions

Previously, $message was accessible because its scope was Global. You can pass variables and values to functions by appending those values to the function call. Also, your function must expect a value. In this example, we pass a string into the function that we declare as $message.

function Write-Message([string]$message)
{
    Write-Host $message
}
# Call the function
Write-Message "Hello World!"
"Hello World!"

Error Handling

Use Try, Catch, and Finally blocks to respond to or handle terminating errors in scripts. The Trap statement can also be used to handle terminating errors in scripts. For more information, see about_Trap.
http://technet.microsoft.com/en-us/library/hh847793.aspx

function Write-Message([string]$message)
{
    Write-Host $message
}

try
{
    # Call the function
    Write-Message "Hello World!"
}
catch [Exception]
{
    # Write exception to host
    Write-Host ("==> Caught Exception: {0}" -f $_.Exception.Message)
}
finally
{
    # Pause the script so that the result can be viewed before the console closes
    Pause
}
"Hello World!"

NOTE: The exception string is built using composite formatting:
{0} is the argument.
-f is the format operator.
$_ is this (keyword).

Further Reading

Scripting with Windows PowerShell

  • Microsoft TechNet’s complete guide to PowerShell.
  • Contains documentation, guides, and related links.

An A-Z Index of Windows PowerShell 2.0 commands

  • SS64 has compiled an alphabetical list of PS v2 commands.

Related Reading

How to Run PowerShell Commands on Remote Computers

  • Hot-To Geek’s guide to enable PowerShell Remoting

Calling PowerShell Function from Command Line

  • StackOverflow Q&A