Skip to content

Logical not operator not working on Where-Object #2658

Description

@Liturgist

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.

Activity

  1. RichardSiddaway commented on Nov 10, 2016

    @RichardSiddaway

    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

  2. Liturgist commented on Nov 10, 2016

    @Liturgist
    Author

    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?

  3. RichardSiddaway commented on Nov 10, 2016

    @RichardSiddaway

    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

  4. added
    Issue-Questionideally support can be provided via other mechanisms, but sometimes folks do open an issue to get a
    on Nov 11, 2016
  5. lzybkr commented on Nov 11, 2016

    @lzybkr
    Contributor

    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 }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

Issue-Questionideally support can be provided via other mechanisms, but sometimes folks do open an issue to get aResolution-By DesignThe reported behavior is by design.

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions