How to use FileInfo object from Powershell
Asked Answered
A

7

22

I am now starting to use PowerShell and after a lot of time using the Unix shells and want to know how to check for the existence of a file or directory.

In Powershell why does Exist return false in the following expression?

PS H:\> ([System.IO.FileInfo]"C:\").Exists
False

And is there a better way to check if a file is a directory than:

PS H:\> ([System.IO.FileInfo]"C:\").Mode.StartsWith("d")
True
Armes answered 11/3, 2009 at 23:57 Comment(0)
M
29

Use Test-Path instead of System.IO.FileInfo.Exists:

PS> Test-Path -Path 'C:\'
True

You can also use -PathType to test whether the location is a file or directory:

PS> Test-Path -Path 'C:\' -PathType Container
True

PS> Test-Path -Path 'C:\' -PathType Leaf
False

DirectoryInfo and FileInfo also both define a PSIsContainer property:

PS> (Get-Item -Path 'C:\').PSIsContainer
True

PS> (Get-Item -Path 'C:\windows\system32\notepad.exe').PSIsContainer
False
Malta answered 12/3, 2009 at 0:8 Comment(0)
T
14

In Powershell why does Exist return false in the following expression?

  PS H:> ([System.IO.FileInfo]"C:\").Exists
  

Because there is no file called "C:\" - it's a directory.

Terbia answered 12/3, 2009 at 20:7 Comment(1)
I'm used to Unix where a directory is a file too.Armes
P
13

In addition to Michael's answer you could also test using:

PS H:> ([System.IO.DirectoryInfo]"C:\").Exists
True
Poky answered 12/3, 2009 at 0:12 Comment(0)
D
10
Help Test-Path

Test-Path Determines whether all elements of a path exist

Test-Path -PathType Leaf C:\test.txt
Test-Path -PathType Container C:\
Test-Path C:\
Dillon answered 12/3, 2009 at 0:12 Comment(0)
H
4

Both of these evaluate to true

$(Get-Item "C:\").GetType() -eq [System.IO.DirectoryInfo]
$(Get-Item "C:\test.txt").GetType() -eq [System.IO.FileInfo]
Hyacinthus answered 4/5, 2017 at 2:56 Comment(0)
S
3

You can use Get-Item to allow PowerShell to select between FileInfo and DirectoryInfo. It will throw an exception if the path doesn't resolve to a location.

PS> $(Get-Item "C:\").GetType()

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     DirectoryInfo                            System.IO.FileSystemInfo

I would only use this over Test-Path if you will need the DirectoryInfo or FileInfo entry if it does exist.

Sihun answered 29/8, 2011 at 20:1 Comment(0)
H
0

I see the best answer above using get-item, but original question used [System.IO.FileInfo] so I'll pose an answer.

[System.IO.FileInfo]"C:\Users\carsonk" | select *

Above will show most of the available attributes not shown in the default table view. There are some PS built-in attributes such as PSIsContainer that won't show by select *

PSIsContainer is not available with [System.IO.FileInfo] so

If(([System.IO.FileInfo]"C:\Users\carsonk").directory) {$True}
True
Hakenkreuz answered 11/7, 2022 at 19:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.