Powershell for Developers
Presentation to the Dot.Net Usergroup Bremen
9 July 2008
Peter Hahndorf
Solution Architect
Hahndorf Consulting
http://peter.hahndorf.eu
The Basics
Keyboard
TAB, F7
Commands
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
.
Aliases
dir, ls are aliases for get-childitems.
get-alias | sort name
Help
get-help Get-History [-detailed] [-full]
get-command
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.
Piping
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
Variables
$variableName, $_
$myName = "Peter"
Write-Host $myName
Write-Host $myName.Replace("P","X")
Functions, Scripts and Profiles
Functions
function demo {Write-Host "Hello $Args"}
demo "World"
Scripts
Set-ExecutionPolicy RemoteSigned
Textfiles with *.ps1 extension
Profiles
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
COM
Access all the COM goodness on your machine; Windows, Office, your own stuff
$ie = new-object -com "InternetExplorer.Application"
$ie.visible = $true
$ie.Navigate2("http://ix.de")
WMI
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
Dot.Net
All the things we like.
Access static methods with ::
$web = [System.Reflection.Assembly]::LoadWithPartialName("System.Web")
$web.GetExportedTypes()
$web.GetExportedTypes() | get-member
$web.GetExportedTypes() | WHERE {$_.Name -like '*Mail*'}
$web.GetExportedTypes() | WHERE {$_.Name -like 'MailMessage'} | get-member
[System.Math]::POW(2,3)
Create an instance of a class
$wc = New-object net.webclient
$page = $wc.DownloadString("http://www.ix.de")
$page.length
$page
$xml = [xml]$wc.DownloadString("http://www.twee.net/news/rss.xml")
$xml.rss.channel.item | select title
Extensions
Many extensions, I use it for email and XML handling.
Tab expansion and 'intellisense' on the command line.
Free Active Directory Management extensions
Exchange 2007
Builtin to the products, does anything the GUI does and more.
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.
Resources
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