PowerShell: Check if a File Exists

There are many ways to skin a cat, and with PowerShell, that’s no exception. I’m going to show you three ways that I’m familiar with to skin check if a file exists.

Hurry! There is not a moment to lose.

© 2017 Microsoft; Nano Man 2: Server of the Realm

TL;DR

Here are the three methods I know in order of my personal preference:

🔼📒 This is my personal preference because of how much other information you now have in the $foo  variable. Read More …

🔼📒 This is great be of how simple, and feature rich this in. This supports wildcards and relational paths. Read More …


🔼📒 Just plain and simple check. No wildcard or relational path support. Read More …

Test-Path

This is probably the most common, and works well for a quick and simple solution to see if a file exists.

📒 I prefer quotes around strings like paths for cleanliness and readability in scripts, but you don’t need the quotes around the path names on any of those commands.

I don’t use quotes in the command line for speed. Powershell is expecting a string so it’ll figure it out:

This can also be used with piping. However, when piping, you definitely need the quotes because PowerShell has no expectations. This is another reason I prefer to just use quotes all the time:

If you want to test if there are any .txt  files in a path or any files with a name that starts with a letter, this is doable with this function using a wildcard:

[System.IO.File]::Exists()

📒 [System]  is the default root type, so things like [System.String]  can be shortcutted to [String] .

I’ll be shortcutting [System.IO.File]  to just [IO.File] . An alternative you can use is the [System.IO.File]::Exists()  which accesses the .NET method to see if the file exists. This is the simplest (aka least feature rich) test, and I’m not really sure why you’d use this over Test-Path . However, it’s here for completeness.

Wildcard tests are not supported by this .NET method.

[System.IO.FileInfo]

📒 [System]  is the default root type, so things like [System.String]  can be shortcutted to [String] .
I’ll be shortcutting [System.IO.FileInfo]  to just [IO.FileInfo] .

Using the [IO.FileInfo]  type is my personal preference when working with files because it gives me a ton of information about the file with a single call. Information that I would have to use a host of tools to get, including:

  • Get-Item
  • Get-ItemProperty
  • Split-Path
  • Test-Path
  • etc

As you just saw, you can check if your referenced file exists with the .Exists  property. However, the object isn’t dynamically updated so you do have to refresh the object to see changes to the file. Using the [IO.FileInfo]  type, you have the added benefit of a bunch of additional file information; go figure:

Additionally, DirectoryName is a [String] , but Directory  is an [IO.DirectoryInfo]  type:

Also, that object’s Parent  property is also an [IO.DirectoryInfo]  type:

Sure, you could just cd ..  to get up a directory and get higher level information. What if you’re programmatically looking through files at a location that’s not your present working directory ( $pwd )? You can easily pull the file’s version of cd ..  with this:

If we were deeper in, we could traverse as high as we want:

Leave a Reply

Your email address will not be published. Required fields are marked *