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
    – jbockle
    Commented May 29, 2013 at 15:34
  • 2
    ie. $(gci -r -fi *.rar) -eq $null
    – jbockle
    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
    – Sabuncu
    Commented May 29, 2013 at 16:56
  • 1
    It is just variable expansion however I add $ just out of habit
    – jbockle
    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.

12
  • 2
    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.
    – Klark
    Commented May 29, 2013 at 17:22
  • 1
    Not sure I agree about the string "false".if('false'){'false is true!'} Commented May 29, 2013 at 17:38
  • 1
    @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.
    – x0n
    Commented May 29, 2013 at 18:11
  • 2
    @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.
    – x0n
    Commented May 29, 2013 at 18:12
  • 2
    @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.
    – x0n
    Commented May 29, 2013 at 19:46
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
  • +1 Thanks. Also, casting null string to bool yields False; i.e. [Bool]"" is false.
    – Sabuncu
    Commented May 29, 2013 at 16:50

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.