Behind the PowerShell Pipeline logo

Behind the PowerShell Pipeline

Subscribe
Archives
November 1, 2022

Collection Filtering and Enumeration

One of PowerShell’s benefits is the ability to manage at scale. Instead of processing one thing at a time, we can manage a bunch of things all at once in the same command.

Get-Childitem c:\work\*.xml |
Where-Object {$_.length -ge 10KB} -pipelinevariable before |
Foreach-Object {
    [xml]$doc = Get-Content $_.fullname
    $doc.InnerXml | Out-File $_.fullname -Encoding utf8
    $new = Get-Item $_.fullname
    Write-Host ("Reduced {0} from {1} to {2} ({3:p2})" -f $before.name,$before.length,$new.length,($new.length/$_.length)) -foreground cyan
}

This code snippet is getting all XML files under C:\Work larger than 10KB and resaving the file with all whitespace stripped out of the XML.

Want to read the full issue?
GitHub Bluesky LinkedIn About Jeff
Powered by Buttondown, the easiest way to start and grow your newsletter.