September 2012

You are browsing the site archives for September 2012.

Recently I stumbled on a piece of PowerShell syntax that confused me massively on first glance

$ some-command |% {some-expression}

It took me a while to find the documentation for that since I was of course looking in the wrong place. It looked to me like some sort of special pipe, when in fact it is just a pipe “|” followed by the ForEach-Object CmdLet which has an alias that (unfortunately) is “%”. So what was actually happening is:

$ some-command | ForEach-Object {some-expression}

For example, I used this to basically fill in some placeholders in a template file

$ Get-Content "mytemplate.txt" |% {$_ -replace "PLACEHOLDER", "42"}