Issue
Calling the Where-Object
cmdlet returns different outputs if the same PSObject
is passed using the pipe or the -InputObject
parameter:
- When using the pipe, the condition is applied.
- When using the
-InputObject
parameter, the condition is ignored.
Question
Why does the condition not apply when the -InputObject
parameter is used?
MWE
Consider the following directory:
PS C:\> Get-ChildItem -Path "C:\tmp\mwe"
Directory: C:\tmp\mwe
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 07/07/2021 15:51 0 a.txt
-a---- 07/07/2021 15:54 0 b.csv
-a---- 07/07/2021 15:53 0 c.log
Using pipe
When piping Get-ChildItem
to Where-Object
, the condition is properly applied and the text file is excluded from the output.
PS C:\> Get-ChildItem -Path "C:\tmp\mwe" | Where-Object -Property "Name" -NotMatch -Value "\.txt"
Directory: C:\tmp\mwe
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 07/07/2021 15:54 0 b.csv
-a---- 07/07/2021 15:53 0 c.log
Using -InputObject
parameter
When using the -InputObject
parameter, the condition is not applied and the text file is not excluded from the output.
PS C:\> Where-Object -InputObject (Get-ChildItem -Path "C:\tmp\mwe") -Property "Name" -NotMatch -Value "\.txt"
Directory: C:\tmp\mwe
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 07/07/2021 15:51 0 a.txt
-a---- 07/07/2021 15:54 0 b.csv
-a---- 07/07/2021 15:53 0 c.log