PowerTip: Using PowerShell to Determine if Path Is to File or Folder

Doctor Scripto

Summary: Learn how to use Windows PowerShell to determine if a path is to a file or a folder.

Hey, Scripting Guy! Question How can I use Windows PowerShell to determine if a path is to a file or a folder?

Hey, Scripting Guy! Answer Use the Get-Item cmdlet to get the object represented by the path. Then use the –Is operator to see if the
           object is a [system.io.directoryinfo] object or a [system.io.fileinfo] object. Here is an example:

PS C:\> (Get-Item c:\fso) -is [System.IO.DirectoryInfo]

True

PS C:\> (Get-Item C:\fso\csidl.txt) -is [System.IO.DirectoryInfo]

False

1 comment

Discussion is closed. Login to edit/delete existing comments.

  • Michael Nadzieja 1

    Or:
    PS C:\> (Get-Item -Path C:\fso).PSIsContainerTrue
    PS C:\> (Get-Item -Path C:\fso\csidl.txt).PSIsContainerFalse

Feedback usabilla icon