You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Logical not operator not working on Where-Object #2658
This is an unfortunate design choice for Where-Object.
The simplified Where-Object relies on the command syntax instead of the expression syntax to gather the property name because the command line arguments are parsed in command mode.
The ! character is not an operator in command mode, e.g. if you wanted to create or read a file with ! in the name, you'd expect to write something like: Get-Context !file.txt.
It might make sense to add a -Not parameter to Where-Object, but I'm not sure it's an improvement over ... | Where-Object { !$_.PSIsContainer }
Activity
RichardSiddaway commentedon Nov 10, 2016
same on PowerShell 5.1
if I remember correctly that structure has always failed
2 options with where-object
original is to use the filter block {}
second is
property operator value
without the {}
You'd need to do something like
Get-ChildItem | where PSisContainer -ne $true
in that case
Liturgist commentedon Nov 10, 2016
Thank you for the workaround Richard.
Using -ne or -eq is reasonable. However, failing silently seems less reasonable. If it is going to fail, shouldn't it fail?
RichardSiddaway commentedon Nov 10, 2016
It didn't fail.
Get-ChildItem | Where-Object
will show you everything that passes the filter. In your case nothing passed the filter so you just get the prompt back
This is the full syntax
Get-ChildItem | Where-Object -Property PSIsContainer -ne -Value $true
When you do
Get-ChildItem | Where-Object -Property PSIsContainer
you have an implicit map to
Get-ChildItem | Where-Object -Property PSIsContainer -eq -Value $true
but
Get-ChildItem | Where-Object -Property !PSIsContainer
doesn't map so your filter passes nothing and you get nothing returned
The one are where the breaks down is when you want to get a negative
For directories & files you're better off using Get-ChildItem's parameters
Get-ChildItem -Directory
Get-ChildItem -File
lzybkr commentedon Nov 11, 2016
This is an unfortunate design choice for
Where-Object.The simplified
Where-Objectrelies on the command syntax instead of the expression syntax to gather the property name because the command line arguments are parsed in command mode.The
!character is not an operator in command mode, e.g. if you wanted to create or read a file with!in the name, you'd expect to write something like:Get-Context !file.txt.It might make sense to add a
-Notparameter toWhere-Object, but I'm not sure it's an improvement over... | Where-Object { !$_.PSIsContainer }