Select-Object versus Write-Output: “The input object cannot be bound to any parameters for the command either because the command does not take pipeline input or the input and its properties do not match any o f the parameters that take pipeline input.”
Posted by jpluimers on 2021/09/23
I bumped in the error [WayBack] “The input object cannot be bound to any parameters for the command either because the command does not take pipeline input or the input and its properties do not match any of the parameters that take pipeline input.” when using [WayBack] Write-Output where [WayBack] Select-Object worked just fine.
This happened when playing around with detecting empty Chocolatey .nupkg package files.
$LibPath = Join-Path $env:ChocolateyInstall 'lib' $NupkgFilter = '*.nupkg' Get-ChildItem -Path $LibPath -Recurse -Filter $NupkgFilter | Where-Object {($_.Length -eq 0) -and ($_.BaseName -eq "hg")} | Sort-Object LastWriteTime | Select-Object BaseName <# Get-ChildItem -Path $LibPath -Recurse -Filter $NupkgFilter | Where-Object {($_.Length -eq 0) -and ($_.BaseName -eq "hg")} | Sort-Object LastWriteTime | Write-Output BaseName ## Write-Output : The input object cannot be bound to any parameters for the command either because the command does not take pipeline input or the input and its properties do not match any of the parameters that take pipeline input. #> Get-ChildItem -Path $LibPath -Recurse -Filter $NupkgFilter | Where-Object {($_.Length -eq 0) -and ($_.BaseName -eq "hg")} | Sort-Object LastWriteTime | ForEach-Object { Write-Output $_.BaseName }
The output is also slightly different, hinting on the root cause:
BaseName -------- hg hg
The above shows that Select-Object selects a list of BaseName properties (italic part), whereas Write-Output shows a single BaseName property content (bold part).
At first I thought the root cause to be that Write-Output expects a single object, but it doesn’t as it also accept a list of objects which you see when comparing both these pieces of documentation:
Sends the specified objects to the next command in the pipeline. If the command is the last command in the pipeline, the objects are displayed in the console.Syntax
Write-Output [-InputObject] <PSObject[]> [-NoEnumerate] [<CommonParameters>]Description
The Write-Output cmdlet sends the specified object down the pipeline to the next command. If the command is the last command in the pipeline, the object is displayed in the console.
Selects objects or object properties.Syntax
Select-Object [-InputObject <PSObject>] [[-Property] <Object[]>] [-ExcludeProperty <String[]>] [-ExpandProperty <String>] [-Unique] [-Last <Int32>] [-First <Int32>] [-Skip <Int32>] [-Wait] [<CommonParameters>]Select-Object [-InputObject <PSObject>] [[-Property] <Object[]>] [-ExcludeProperty <String[]>] [-ExpandProperty <String>] [-Unique] [-SkipLast <Int32>] [<CommonParameters>]Select-Object [-InputObject <PSObject>] [-Unique] [-Wait] [-Index <Int32[]>] [<CommonParameters>]Description
The
Select-Objectcmdlet selects specified properties of an object or set of objects. It can also select unique objects, a specified number of objects, or objects in a specified position in an array.
The actual difference is that Select-Object understands property names, but Write-Object does not.
Workaround
Since there is no $_ inside the pipeline, you need a ForEach-Object which does:
Get-ChildItem -Path $LibPath -Recurse -Filter $NupkgFilter | Where-Object {($_.Length -eq 0) -and ($_.BaseName -eq "hg")} | Sort-Object LastWriteTime | ForEach-Object { Write-Output $_.BaseName }
Related
- [WayBack] Understanding PowerShell pipelines – PowerShell | Microsoft Docs
- [WayBack] PowerShell Basics: 4 Pipeline | Richard Siddaway’s Blog
- [WayBack] PowerShell – Troubleshooting pipeline input – CodingBee
- [WayBack] powershell – Move-Item : The input object cannot be bound to any parameters for the command either because the command does not take pipeline input? – Stack Overflow
- [WayBack] Topic: handle input from pipeline | PowerShell.org
–jeroen






Leave a comment