PowerShell Tool Output
Tool output is what a tool prints for its reader. Objects suit the pipeline, plain text suits a human reader, and the information stream suits progress notes.
Keep objects in the pipeline
File objects carry a size property. Filter on that property before printing only the names.
Set-Content -Path "a.txt" -Value "a"
Set-Content -Path "b.txt" -Value "beta beta"
Get-ChildItem -Filter *.txt | Where-Object { $_.Length -gt 5 } | Sort-Object Name | Select-Object -ExpandProperty NameBuild one readable line for a human
Join the names into one line with a label in front. The line reads as a sentence fragment for a person.
Set-Content -Path "a.txt" -Value "a"
Set-Content -Path "b.txt" -Value "b"
$names = Get-ChildItem -Filter *.txt | Sort-Object Name | Select-Object -ExpandProperty Name
"Files: " + ($names -join ", ")Send a progress note on the information stream
Set the information preference to Continue so the note appears. The note prints before the final result.
$InformationPreference = "Continue"
Write-Information "Starting check"
"Check done"Example
Set-Content -Path "a.txt" -Value "a"
Set-Content -Path "b.txt" -Value "beta beta"
$InformationPreference = "Continue"
Write-Information "Scanning files"
$names = Get-ChildItem -Filter *.txt | Where-Object { $_.Length -gt 5 } | Sort-Object Name | Select-Object -ExpandProperty Name
"Files over 5 bytes: " + ($names -join ", ")Key points
- Pipeline object: An object in the pipeline carries properties that later commands can filter and sort.
- Human text: Plain text is built for reading, so it joins values into one readable line.
- Information stream: Write-Information sends a progress note on the information stream.
- Silent by default: Write-Information output stays silent unless the information preference is set to Continue.
- Print values: Print single properties or joined text, never a whole object, so the output stays stable.
In this example, the script first sends a progress note on the information stream. It then keeps the file objects in the pipeline and filters them by size. Finally it builds one readable line from the surviving names and prints it for a human reader.
Note
A tool can send objects down the pipeline and still print readable text for the person running it.
Try it Yourself
Edit the code, then press Run.
Running is simulated
Exercise
A tool should print a progress note that does not become pipeline data. Which command sends that kind of note?