PowerShell: Check if File Exists

If you writing a PowerShell Script and you want to check if a file exists or not, you can make use of the Test-Path cmdlet from the Microsoft.PowerShell.Management module.

Syntax:

Test-Path
    [-Path] <String[]>
    [-Filter <String>]
    [-Include <String[]>]
    [-Exclude <String[]>]
    [-PathType <TestPathType>]
    [-IsValid]
    [-Credential <PSCredential>]
    [-OlderThan <DateTime>]
    [-NewerThan <DateTime>]
    [<CommonParameters>]

Example:

Test-Path 'C:\data\user\alan\records.csv'

False

You will see a boolean True|False based on whether the file exits or not.

PowerShell Check if File Exists or Not

Comments & Discussion

Facing issues? Have questions? Post them here! We're happy to help!