Check if a File Exists using PowerShell

There are multiple methods to check if a file exists using PowerShell. In this blog post, we’ll explore two common approaches: using the Test-Path cmdlet and utilising .NET methods.

Using the Test-Path Cmdlet to Check if a File Exists

The Test-Path cmdlet is a straightforward and effective way to check if a file exists in Windows PowerShell. It returns $true if the file exists and $false if it doesn’t. Here’s a basic example:

$filePath = "C:\Alkane\Alkane.txt"
if (Test-Path $filePath) {
Write-Host "File exists at $filePath"
} else {
Write-Host "File does not exist at $filePath"
}

In this example, we assign the file path to the $filePath variable and use Test-Path to check its existence. The script then prints an appropriate message based on the result.

Using .NET Methods to Check if a File Exists

Another way to check if a file exists in Windows PowerShell is by utilising .NET methods. You can use the [System.IO.File]::Exists() method to achieve this. Here’s an example:

$filePath = "C:\Alkane\Alkane.txt"
if ([System.IO.File]::Exists($filePath)) {
Write-Host "File exists at $filePath"
} else {
Write-Host "File does not exist at $filePath"
}

In this script, we use the [System.IO.File]::Exists() method to check if the file exists, similar to the Test-Path approach. It also prints the appropriate message based on the result.

Use Test-Path with PowerShell

When we want to check for the existence of files, folders and registry entries we can use Test-Path with PowerShell to return ‘true’ if it exists and ‘false’ if it doesn’t exist.

Using Test-Path with Files and Folders

Here we test against the following folder structure:

c:\alkane (folder)
c:\alkane\alkane.txt (file)
c:\alkane\alkane.ps1 (file)

And provide the following quick-start examples:

#does folder exist
write-host (Test-Path -Path c:\alkane)
#returns true
#does file exist
write-host (Test-Path -Path c:\alkane\alkane.txt)
#returns true
#is c:\alkane\alkane.txt a file?
write-host (Test-Path -Path c:\alkane\alkane.txt -PathType Leaf)
#returns true
#is c:\alkane\alkane.txt a folder?
write-host (Test-Path -Path c:\alkane\alkane.txt -PathType Container)
#returns false
#is c:\alkane\alkane.txt newer than August 20, 2022 (today is September 13, 2022!)
write-host (Test-Path -Path c:\alkane\alkane.txt -NewerThan "August 20, 2022") 
#returns true
#is c:\alkane\alkane.txt newer than September 20, 2022 (today is September 13, 2022!)
write-host (Test-Path -Path c:\alkane\alkane.txt -NewerThan "September 20, 2022") 
#returns false
#does the folder contain any ps1 (PowerShell) file
write-host (Test-Path -Path c:\alkane\*.ps1)
#returns true
#does the folder contain any file with the letters 'ane' in it
write-host (Test-Path -Path c:\alkane\*ane*)
#returns true
#does c:\alkane contain any files other than ps1 files?
write-host (Test-Path -Path c:\alkane\* -exclude *.ps1)
#returns true

Using Test-Path with the Registry

Here we test against the following registry structure:

HKLM\SOFTWARE\Alkane (registry key)
HKLM\SOFTWARE\Alkane\AlkaneValue (registry value)

And provide the following quick-start examples:

#does registry key exist
write-host (Test-Path -Path HKLM:\SOFTWARE\Alkane)
#returns true
#does registry value AlkaneValue exist
write-host (Test-Path -Path HKLM:\SOFTWARE\Alkane\AlkaneValue)
#returns false - WRONG BECAUSE IT DOES EXIST!  Test-Path does not work with registry values.  
#We must work a bit harder to see if a registry value exists.
#Use the below example instead
#does registry value AlkaneValue exist
write-host $(if ((Get-ItemProperty -Path HKLM:\SOFTWARE\Alkane\ | Select-Object -ExpandProperty AlkaneValue -ErrorAction SilentlyContinue) -eq $null) { $false } else { $true })
#returns true