PowerTip: Looking for Folders and Files in PowerShell

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

Comments (1)

  1. umark says:

    Hi Scripting Guy; Thanks for the brilliant info. I have an issue with out put file. Am currently doing a cleanup on my server by lookingup files last modified by date xxx, the lookup is working fine but my .txt file is the issue. Notepad has a limitation
    of 58MB but my file is 1.5GB. Qn. is there any other output file i can use to handle the size or a script to split the file in readable .txt (58MB file)
    umar k

Skip to main content