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
-

Facing issues? Have Questions? Post them here! I am happy to answer!


Author: Rakesh
Author Info:

Rakesh is a seasoned developer with over 10 years of experience in web and app development, and a deep knowledge of operating systems. Author of insightful How-To articles for Code2care.

Follow him on: X

Copyright © Code2care 2023 | Privacy Policy | About Us | Contact Us | Sitemap