Check if a file exists or not in Windows PowerShell? Check if a file exists or not in Windows PowerShell? powershell powershell

Check if a file exists or not in Windows PowerShell?


Just to offer the alternative to the Test-Path cmdlet (since nobody mentioned it):

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

Does (almost) the same thing as

Test-Path $path -PathType Leaf

except no support for wildcard characters


Use Test-Path:

if (!(Test-Path $exactadminfile) -and !(Test-Path $userfile)) {  Write-Warning "$userFile absent from both locations"}

Placing the above code in your ForEach loop should do what you want


You want to use Test-Path:

Test-Path <path to file> -PathType Leaf