This is the Bible. One of the designers of powershell wrote it. If you want to read the powershell documentation, this is it. There's things in here that aren't even documented online. There's some little mistakes here and there. I wish it was less expensive and more widely known. Copying and pasting from the pdf isn't great. The forum that comes with the book isn't that active. Read the book and impress and provoke arguments with the people on stackoverflow. Powershell is very flexible. You can creatively bend it to your will. It comes with windows. I think CS intro classes should use it.
How nerdy is this? Some favorite bits of code (or inspired by):
$a,$b = 1,2
$a = $b = 2
$a,$b = $b,$a
(1,2,3)[-1] # get last one
${c:file1.txt},${c:file2.txt} = ${c:file2.txt},${c:file1.txt} # swap files
${c:file.txt} = ${c:file.txt} -replace 'old','new' # can 'file.txt' come from another var?
get-childitem | foreach {$sum = 0}{$sum++}{$sum}
invoke-command localhost,localhost,localhost {sleep 5;'done'} # concurrent, if I could fix access perms
(Get-Process notepad).foreach('Kill')
ps | foreach name
'hi.there' | foreach split .
(ps | sort Handles).where({$_.Handles -gt 1000}, 'Until')
[void] (echo discard me)
$(echo hi; echo there) | measure
using assembly System.Windows.Forms
using namespace System.Windows.Forms
[messagebox]::show('hello world')
$err = $( $output = ls foo3 ) 2>&1
if ( $(if (1 -eq 1) {'yes'}) -eq 'yes' ) { 'yep' }
switch(1,2,3,4,5,6) {
{$_ % 2} {"Odd $_"; continue}
4 {'FOUR'}
default {"Even $_"}
}
get-content variable:a | set-content -value 'there'
& { 'inline func' }
$function:foo = {'Bye!'}
workflow work {
foreach -parallel ($i in 1..3) {
sleep 5
"$i done"
}
}
work
# this looks cool
$a = start -NoNewWindow powershell {timeout 10; '1 done'}
$b = start -NoNewWindow powershell {timeout 10; '2 done'}
$c = start -NoNewWindow powershell {timeout 10; '3 done'}
$a,$b,$c | wait-process
netstat -n | select -Skip 4 |
ConvertFrom-String -pn Blank, Protocol,
LocalAddress, ForeignAddress, State |
Select Protocol, LocalAddress, ForeignAddress, State
notepad | echo # wait for it
1..5 | &{process{$_ * 2}}
[flags()] enum bits {one = 1; two = 2; three = 4}
[bits]7
$p = [PowerShell]::Create() # make another runspace
[void] $p.AddScript{Sleep 5; ls foo5; 'done'}
$r = $p.BeginInvoke(); # run in background
$p.EndInvoke($r) # wait
$p.streams.error # see any errors
$p.dispose()
# get and set a variable in a module scope
$m = get-module counter
& $m Get-Variable count
& $m Set-Variable count 33
start-threadjob { sleep 5; 'done' } # PS 6