4

Please consider the following PowerShell assignment statement:

$rc = (gci -r -fi *.rar)

If there is a rar file present in the directory structure, then echo $? displays the following:

    Directory: C:\file tests

Mode                LastWriteTime     Length Name
----                -------------     ------ ----
-a---         7/22/2012   7:09 PM    3699776 somefile.rar

Fine. Now consider this PowerShell if statement:

if (gci -r -fi *.rar)
{
    echo "Rar files found"
}
else 
{
    echo "No rar files"
}

In the if statement, the return from the gci cmdlet is treated as a boolean value. But the return from the same cmdlet produced a string output in case of the earlier assignment statement.

I know that PS is an object shell. I understand cmdlets act differently depending on context. But what I don't understand is how this is accomplished, and what mechanisms are used.

Specifically: What is the magic used by the if statement that allows the return from gci to be treated as a boolean? If I wanted to use that mechanism elsewhere (outside of an if statement), what would I have to do? Is there some sort of a "cast to boolean" operator? E.g.

$rc = (Cast following to boolean)(gci -r -fi *.rar)
4
  • 1
    it's not returning boolean but is returning that it is either $null or not $null Commented May 29, 2013 at 15:34
  • 2
    ie. $(gci -r -fi *.rar) -eq $null Commented May 29, 2013 at 15:36
  • @jbockle: Upped your comments, thank you. What does the $(...) surrounding the gci -r -fi *.rar do? I have tried it without the dollar sign, and it still works. I.e. (gci -r -fi *.rar) -eq $null Commented May 29, 2013 at 16:56
  • 1
    It is just variable expansion however I add $ just out of habit Commented May 29, 2013 at 17:25

2 Answers 2

9

This table gives you the answer.

True                                         False
~~~~                                         ~~~~~
$TRUE                                        $FALSE
Any string of length > 0                     Empty string
Any number ≠ 0                               Any number = 0
Array of length > 1                          Array of length 0
Array of length 1 whose element is true      Array of length 1 whose element is false
A reference to any object  <<<<<             $NULL

Idea behind this is to use similar checks to verify if an object is initialized properly.

Sign up to request clarification or add additional context in comments.

12 Comments

gci can either return null or FileSystemInfo or Array of FileSystemInfo. So it depends on your folder content. If have only one file it will be true due to 'A reference to any object' rule. If you get an array it will be true due to Array of length > rule. If you get null it will be false due to Null rule.
Not sure I agree about the string "false".if('false'){'false is true!'}
@MikeShepard Don't confuse the english language with the powershell language. How should such a script run on a French locale? Should it do the same for "vrai" ? There are no keywords in strings, just strings.
@Sabuncu - there are no strings returned from gci/dir. You only see that when the objects are not captured (in a variable or piped to a cmdlet) and end up being sent as output to the formatter for visual consumption.
@Sabuncu Yes. Anything that's not null, not 0, not $false and not an empty collection is considered $true. This is very useful generally speaking and is not just a powershell thing. Javascript has this concept of "truthiness" too.
|
1

I think Powershell just treats null and zero as false like so:

PS > if(1) {"True"} else {"False"}
True
PS > if(0) {"True"} else {"False"}
False
PS > if($null) {"True"} else {"False"}
False

To cast use the following format:

PS > [Bool]"1"
True

1 Comment

+1 Thanks. Also, casting null string to bool yields False; i.e. [Bool]"" is false.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.