Examine Parameter Binding

by Dec 14, 2012

PowerShell caters all tastes which is why the next two lines do the exact same thing and get all JPG pictures from your Windows folder:

PS> Get-ChildItem -Path $env:windir -Filter *.jpg -Name -Recurse -ErrorAction SilentlyContinue
PS> ls -r $env:windir -n *.jpg -ea 0 

The first line is "politically correct" and uses named parameters and cmdlet names. The second trusts in positional parameters and aliases, and PowerShell internally "binds" the arguments to the correct parameters. If you'd like to see how this magic is done (and maybe want to "decipher" a cryptic line of PowerShell code yourself), use Trace-Command like this:

PS> Trace-Command -PSHost -Name ParameterBinding { ls -r $env:windir -n *.jpg -ea 0 }

Look for lines that start with "BIND" and end with "SUCCESSFUL"!

Twitter This Tip! ReTweet this Tip!