This isn’t really a question. But it seems to me there’s a disconnect between Bruce Payette’s book and the official documentation. I had no idea I could do all this stuff. I guess some are in the docs, but are not done commonly. Although I would never use ‘>’ and ‘>>’, since they encode in unicode.
# where method (and foreach method) which are faster
(Get-Process | sort Handles).where({$_.Handles -gt 1000}, 'Until')
(Get-Process notepad).foreach('Kill')
# hide output with [void]
[void] $(Write-Output "discard me")
# invoke a method with a variable
$method = 'sin'
[math]::$method.Invoke(3.14)
# swap two variables:
$a,$b = $b,$a
# reference any psdrive like C: with a variable, but need the curly braces {}
# (but how do I pass in "$path = 'file.txt'?)
# and search and replace a file
${c:file.txt} = ${c:file.txt} -replace 'oldstring','newstring'
# and swap two files
${c:file1.txt},${c:file2.txt} = ${c:file2.txt},${c:file1.txt}
# $foreach and movenext, ok this is in the doc but I didn't know it
foreach ($i in 1..10)
{ [void] $foreach.MoveNext(); $i + $foreach.Current }
# foreach-object operation statement, in docs but didn't know
ps | foreach-object name
'hi.there' | Foreach-object Split .
# using assemply (didn't know using namespace)
using assembly System.Windows.Forms
using namespace System.Windows.Forms
[messagebox]::show('hello world')
# an assignment is an expression
if (2 -eq ($a = 2)) { 'yes' }
# and then with redirection save output and error to separate variables
$err = $( $output = ls foo3 ) 2>&1
# restrict var to int, in doc but didn't know
[int]$a = 1
# restrict length of var like in advanced functions
[ValidateLength(0,5)] [string] $cv = ''
# subexpressions $() aren't just for double quoted strings,
# you can put in multiple statements with semicolons and keywords like if and foreach
# and put them anywhere you can put an expression or pipeline
$( echo hi; echo there ) | measure
if ( $(if (1 -eq 1) {'yes'}) -eq 'yes' ) { 'yep' }
for ($i=0; $($y = $i*2; $i -lt 5); $i++) { $y }
# labels, in doc but didn't know
# loop label, break out of both loops
$target = 'outer'
:outer while (1) {
while(1) {
break $target
}
}
# switch, so many things, arrays and $_, in doc but didn't know
switch(1,2,3,4,5,6) {
{$_ % 2} {"Odd $_"; continue}
4 {'FOUR'}
default {"Even $_"}
}
# $switch and movenext and current
switch ($options){
'-a' { $a=$true }
'-b' { [void] $switch.MoveNext(); $b= $switch.Current }
'-c' { $c=$true }
'-d' { $d=$true }
}
# three script blocks with foreach-object -process arg (for begin, process, end)
gps svchost | foreach-object {$t=0} {$t+=$_.handles} {$t}
# -replace has a lot of codes for the 2nd arg,
# doc is in some regex doc that's not linked from the about comparison operators page
$number Substitutes the last submatch matched by group number.
${name} Substitutes the last submatch matched by a named capture of the form (?<name>).
$$ Substitutes a single "$" literal.
$& Substitutes a copy of the entire match itself.
$` Substitutes all the text from the argument string before the matching portion.
$' Substitutes all the text of the argument string after the matching portion.
$+ Substitutes the last submatch captured.
$_ Substitutes the entire argument string.
# pipes in simple funcs ($input has a .movenext() too)
function sum {
$total=0;
foreach ($n in $input) { $total += $n }
$total
}
1,2,3 | sum