July 3rd, 2014

PowerTip: Looking for Folders and Files in PowerShell

Doctor Scripto
Scripter

Summary: Learn about looking for folders and files in Windows PowerShell.

Hey, Scripting Guy! Question Why does this command show me folders instead of files?

Dir | Where PSIsContainer -eq False

Hey, Scripting Guy! Answer In Windows PowerShell, use the automatic variables $True and $False to refer to Boolean values
          instead of the string True and False. To perform the comparison, Windows PowerShell converts
          the object on the right into the same type as on the left, so the string False is evaluated as a Boolean True.

In this case, you are converting a string into a Boolean value, so if the string has characters in it, it will
become a True. Only an empty string becomes a False. This is why your sample code gives you folders
instead of files. Try it like this and you will be a lot happier with the results:

Dir | Where PSIsContainer -EQ $False

Author

The "Scripting Guys" is a historical title passed from scripter to scripter. The current revision has morphed into our good friend Doctor Scripto who has been with us since the very beginning.

0 comments

Discussion are closed.