Example Shell
Powershell for Developers
Presentation to the Dot.Net Usergroup Bremen
9 July 2008
Peter Hahndorf
Solution Architect
Hahndorf Consulting


  • Requirements
  • The Basics
  • Functions, Scripts and Profiles
  • COM, WMI and Dot.Net
  • Examples
  • Building Cmdlets
  • Extensions
  • Preview of Version 2.0
  • Resources


  • Requires .Net 2.0 but doesn't run on Windows 2000
  • 32 and 64 bit Versions, not all extensions run under 64 Bit
  • Different downloads for XP, Windows 2003 and Vista.
    Included in Server 2008

The Basics




Command -parameter1 -parameter2 argument1 argument2 Arguments are parameters to real parameters

Always verb-noun combinations, so no dir or copy but get-childitems and copy-item.


dir, ls are aliases for get-childitems. get-alias | sort name


get-help Get-History [-detailed] [-full]
Get-Process | get-member

Providers and drives

Filesystem, Registry are builtin, as extensions: Exchange mailboxes, IIS sites etc. get-psdrive
set-location HKLM:
sl hkcu: ls, cd into stuff
sl Function:
sl C:

You can add your own drives as subset of existing ones, or even write your own providers for your application.


Passing output as input to other commands. Everything is an object get-process
get-process | where {$_.ProcessName -eq 'Powershell'}
get-process | where {$_.ProcessName -eq 'Powershell'} | select ProcessName, CPU
get-childitem | where {$_.Name -like '*.exe'}
get-childitem | where {$_.Name -like 'zip.exe'}
get-childitem | where {$_.Name -like 'zip.exe'} | select *
get-childitem | where {$_.Name -like 'zip.exe'} | get-member
get-childitem | where {$_.Name -like 'zip.exe'} | get-member | where {$_.Name -eq 'FullName' } | select * | fl


$variableName, $_
$myName = "Peter"
Write-Host $myName
Write-Host $myName.Replace("P","X")

Functions, Scripts and Profiles


function demo {Write-Host "Hello $Args"}
demo "World"


Set-ExecutionPolicy RemoteSigned
Textfiles with *.ps1 extension


Start when Powershell starts:
Per user: %UserProfile%\(My )Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1
Per machine: %windir%\system32\WindowsPowerShell\v1.0\ Microsoft.PowerShell_profile.ps1

COM, WMI and Dot.Net


Access all the COM goodness on your machine; Windows, Office, your own stuff $ie = new-object -com "InternetExplorer.Application"
$ie.visible = $true


Some things are only available through Windows Management Instrumentation Get-WmiObject -class Win32_LogicalDisk | sort -desc Size | select -first 5 DeviceID, VolumeName, Size | format-table -autosize


All the things we like.
Access static methods with ::
$web = [System.Reflection.Assembly]::LoadWithPartialName("System.Web")
$web.GetExportedTypes() | get-member
$web.GetExportedTypes() | WHERE {$_.Name -like '*Mail*'}
$web.GetExportedTypes() | WHERE {$_.Name -like 'MailMessage'} | get-member
Create an instance of a class
$wc = New-object net.webclient
$page = $wc.DownloadString("http://www.ix.de")
$xml = [xml]$wc.DownloadString("http://www.twee.net/news/rss.xml")
$xml.rss.channel.item | select title


  • Remove all Subversion files from a folder
  • cd into location
    get-childitem . -recurse -filter *.svn -force
    ls . -recurse -filter *.svn -force | remove-item -recurse -whatif
  • Set file dates based on original file dates
  • Use \Data\Presentations\Powershell\TouchDemo\TouchDemo.ps1
  • Prepare asp.net deployment
  • Creates Deployment Zip File to be copied to server
    - Compiles Web-Sites into multiple assemblies
    - Removes Theme Attributes from Page Directives
    - Merges all web assemblies into a single one
    - Compares new version with last version and
    finds all the different files
    - Zips the new files into a special zip file.
    Build an Assembly
    Change aspx.cs
    Line 46
    Update SolutionInfo.cs

    Show 'Include Files' in packagetopas.ps1
    Show 'Compare' in common.ps1 Line: 65

Building CmdLets

  • Download and install VS Templates
  • Write 'Hello World' Cmdlet
  • Create a new Powershell Project
    Add item PSCmdlet
    Implement ProcessRecord
    WriteWarning("Hello Bremen .Net User Group");
    Set name 'GroupGreeting' in attribute
    Set SnapIn Name 'BremenUG' in PSSnapin.cs
    Compile Project
    Copy dll path
    (as admin) cd $tnDiexit rDotNet2
    .\InstallUtil.exe "full assemblyName"
    get-pssnapin -registered
    add-pssnapin BremenUG


PowerShell Community Extensions

Many extensions, I use it for email and XML handling.


Tab expansion and 'intellisense' on the command line.

PowerShell Commands for Active Directory

Free Active Directory Management extensions

Exchange 2007

Builtin to the products, does anything the GUI does and more.

PowerShell Provider for IIS 7.0

Full management of IIS plus real time request monitoring.

SQL Server 2008

Builtin, special version

Other Microsoft Products

All future Microsoft server products will have builtin Powershell support.

Preview of Version 2.0

  • Graphical Shell, editor with integrated shell
  • Transactions
  • Many new commandlets
  • Remote script execution, but only on Windows 6 with WinRM


Windows Powershell in Action

Best Powershell book I've read, by Bruce Payette on Manning

PowerShell Anwendergruppe

Microsoft Powershell Home

Powershell @ Microsoft Script Center

My blog