Repository navigation
Logical not operator not working on Where-Object #2658
Description
Activity
same on PowerShell 5.1
if I remember correctly that structure has always failed2 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 caseThank 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?
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 $trueWhen you do
Get-ChildItem | Where-Object -Property PSIsContaineryou have an implicit map to
Get-ChildItem | Where-Object -Property PSIsContainer -eq -Value $truebut
Get-ChildItem | Where-Object -Property !PSIsContainerdoesn'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- addedIssue-Questionideally support can be provided via other mechanisms, but sometimes folks do open an issue to get aideally support can be provided via other mechanisms, but sometimes folks do open an issue to get a
on 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 }- addedResolution-AnsweredThe question is answered.The question is answered.
on Dec 8, 2016 - addedResolution-By DesignThe reported behavior is by design.The reported behavior is by design.and removedResolution-AnsweredThe question is answered.The question is answered.
on Jan 12, 2017
It seems odd that applying the logical not operator does not work when used outside of a ScriptBlock.
Get-ChildItem | Where-Object PSIsContainer # works
Get-ChildItem | Where-Object !PSIsContainer # fails silently
Get-ChildItem | Where-Object {$.PSIsContainer} # works
Get-ChildItem | Where-Object {!$.PSIsContainer} # works
Windows 10, GitCommitId v6.0.0-alpha.12
Ubuntu 16.04, GitCommitId v6.0.0-alpha.12
I would label this as Area-Language, but it appears that I cannot set a label.