PowerShell for Windows Admins

Sep 8 2018   6:15AM GMT

PowerShell check file exists

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

Tags:
Powershell

There are times when you need to check if a file exists – this is how you do a PowerShell check file exists.

If you try to access a file that doesn’t exist you’ll get an error:

PS> Get-Content -Path c:\test\z27.txt
Get-Content : Cannot find path ‘C:\test\z27.txt’ because it does not exist.
At line:1 char:1
+ Get-Content -Path c:\test\z27.txt
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : ObjectNotFound: (C:\test\z27.txt:String) [Get-Content], ItemNotFoundException
+ FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetContentCommand

Rather than manage the error directly you can test to see if the file exists

PS> Test-Path -Path c:\test\z27.txt
False

Test-Path returns a boolean (true or false) so you can then test the file exists before accessing

PS> if (Test-Path -Path c:\test\z27.txt) {Get-Content -Path c:\test\z27.txt}

You’d obviously add an else block to deal with the case of the file not existing – possibly just a warning or a throw depending on what you were doing,

You can also test if a folder exists

PS> Test-Path -Path c:\test\
True
PS> Test-Path -Path c:\test27\
False

You can be more exact with whether you’re dealing with a folder or a file by using the –PathType parameter

PS> Test-Path -Path c:\test -PathType Any
True
PS> Test-Path -Path c:\test -PathType Container
True
PS> Test-Path -Path c:\test -PathType Leaf
False
PS> Test-Path -Path C:\test\p1.txt -PathType Container
False
PS> Test-Path -Path C:\test\p1.txt -PathType Leaf
True

Use container for a folder and leaf for a file.

Alternatively you can use –Filter, –Include and –Exclude but remember you’ll only get a boolean returned so

PS> Test-Path -Path C:\test\* -Filter *.txt
True

gives a single return even though there are 20 .txt files in the folder!

Your last alternative is to –OlderThan or –NewerThan to test against a files age

PS> Test-Path C:\test\* -Filter *.txt -NewerThan ((Get-Date).AddDays(-3))
False
PS> Test-Path C:\test\* -Filter *.txt -OlderThan ((Get-Date).AddDays(-7))
True

I do have .txt files older than 7 days but none younger than 3 days old.

The –LiteralPath parameter  means that the path is used exactly as given and ignores any possible interpretation of a character as anything else. use it instead of –Path.

Finally –IsValid tests the validity of the path syntax even if the path doesn’t exist so

PS> Test-Path -Path C:\test\z27.txt
False
PS> Test-Path -Path C:\test\z27.txt -IsValid
True

The file itself doesn’t exist but the path is valid.  Conversely

PS> Test-Path -Path C:\test\p1.txt
True
PS> Test-Path -Path C:\test\p1.txt -IsValid
True
PS> Test-Path -Path C:\test\p?1.txt -IsValid
False
PS> Test-Path -LiteralPath C:\test\p?1.txt -IsValid
False

p1.txt exists and has a valid path but p?1.txt isn’t a valid path.

On Windows folder and file names are case INSENSITIVE so

PS> Test-Path -Path C:\test\p1.txt
True
PS> Test-Path -Path C:\tEsT\p1.txt
True

both work.

On Linux folder and file names are case sensitive so be aware. How to deal with this situation is currently under discussion

https://github.com/PowerShell/PowerShell/issues/7578

 Comment on this Post

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when other members comment.

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

Share this item with your network: