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 1,847 other followers

PowerShell: 3 ways of `not` (except `not` itself: that doesn’t work) via Stack Overflow

Posted by jpluimers on 2014/11/05

Thanks Ryant for posting this answer, which I’ve paraphrased a bit:

not itself is not a boolean operator in PowerShell.

The reason is that PowerShell borrows a lot from *nix history, and this has been in *nix forever.

So there are 3 ways of negating a boolean, all equivalent:

-Not

if (-Not (Test-Path C:\Code)) {
    write "it doesn't exist!"
}

!

The exclamation mark (!) is an alias for -Not.

if (!(Test-Path C:\Code)) {
    write "it doesn't exist!"
}

-bxor 1

Just for fun, you could also use bitwise exclusive or -bxor, though it’s not the most readable/understandable method.

if ((test-path C:\code) -bxor 1) {
    write "it doesn't exist!"
}

Aliasing

-not is the only logical operator that comes with an alternate and operators cannot be aliased.

Fazit

The exclamation mark is the only aliased boolean operator.

Further reading

(see help about_Logical_Operators and help about_Comparison_Operators),

–jeroen

via: windows – How do I negate a condition in powershell? – Stack Overflow.

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: