The Wiert Corner – irregular stream of stuff

Jeroen W. Pluimers on .NET, C#, Delphi, databases, and personal interests

  • My badges

  • Twitter Updates

  • My Flickr Stream

  • Pages

  • All categories

  • Enter your email address to subscribe to this blog and receive notifications of new posts by email.

    Join 2,482 other followers

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:

[WayBack] Write-Output

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.

[WayBack] Select-Object

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-Object cmdlet 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

–jeroen

 

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

 
%d bloggers like this: