Powershell

How to use PowerShell to check if a file exists?

Creation and deletion of files is a common practice in computer programming, especially in scripting languages. Before creating or doing any operation on a file, the existence of the file must be ensured.

By doing so, you can pre-check the existence of the file and can avoid the “file not found” error in PowerShell. PowerShell provides four different ways to check if the path exists or not. These methods include Test-Path, Get-ChildItem, Get-Item, and System.IO.File]::Exists method.

In this article, we will cover the four different methods used in PowerShell to check if a file exists. The outcomes of the article are:

  • How to use the Test-Path cmdlet?
  • How to use the Get-ChildItem cmdlet?
  • How to use the Get-item cmdlet?
  • How to use the System.IO.File method?

Method 1: How to check the existence of a file using the PowerShell “Test-Path” cmdlet?

The presence of any file can be checked through the use of the “Test-Path” cmdlet. This command can be utilized either using an absolute path (complete path of the file) or a relative path (path of the file according to the present working directory). It is to be noted that the Test-Path returns the output in True or False only.

Test-Path cmdlet with Relative Path
The below command will be run to check the “file.rtf” within the present working directory. The parameter “Pathtype Leaf” here refers to the file rather than the folder/directory.

Test-Path "file.rtf" -PathType Leaf

The returned output is false, which states that the file does not exist in the present directory.

In the following Test-Path command we are using the wildcard (*) with file extension (.rtf) to check whether the “.rtf” files exist or not.

Test-Path "*.rtf" -PathType Leaf

It can be noticed that the files with the “.rtf” extension are present in the Test directory.

Test-Path cmdlet with absolute path
If you are in the root directory or elsewhere, the use of the absolute path can be very useful. Use the following (i.e. Test-Path -Path “ ”) command to find out if the “.rtf” files do exist in the Test folder or not.

Test-Path -Path "C:\Users\powershell\Test\*.rtf" -PathType Leaf

It is shown in the given-above snippet that the “.rtf” files are available on the particular path.

Method 2: How to Check if a File exists using PowerShell “Get-ChildItem” cmdlet?

PowerShell Get-ChildItem can also be used to check the availability of a particular file(s). In the below example we will see the practical demonstration of the Get-ChildItem cmdlet. Suppose we want to know about the presence of the test.txt file. The Get-ChildItem command that is used is as follows.

Get-ChildItem -Path C:\Users\powershell\Test\test.txt

As you can observe from the above message that the particular path does not contain the test.txt file.

Apart from this, We can also use the wildcard (*) with file extension to check if any file with the extension exists. Here is the command to check the existence of files with the “.txt” extension.

Get-ChildItem -Path C:\Users\powershell\Test\*.txt

As per the above output screenshot, there are two “text” files available in the Test directory.

Method 3: How to Check if a File exists using the PowerShell “Get-Item” cmdlet?

Powershell supports the Get-Item command for checking files in the directory. The following example will demonstrate the practical implementation of the Get-Item command. For instance, the availability of the file.jpg will be checked with the following command.

Get-Item C:\Users\powershell\Test\file.jpg

The output shows that there is no file existing with the name “file.jpg”. But the wildcard can be used if you do not remember the exact name of the file.

Get-Item C:\Users\powershell\Test\*.jpg

It can be noticed in the above-attached screenshot, that there are two JPG files in the Test folder

Method 4: How to Check File through “System.IO.File::Exists” cmdlet?

[System.IO.File]::Exists is also one of the methods used in PowerShell to manage various operations (creation, deletion, copying, moving) on files. The return type of this method is Boolean(True/False).

In the example command, the System.IO.file method is applied to a file named “file5.doc” to check its existence using the absolute path of the file.

[System.IO.File]::Exists("C:\Users\powershell\Test\file5.doc")

As you can see, file5.doc does exist in the particular folder.

Note:
Like other methods such as Test-Path, Get-ChildItem, and Get-Item, the wildcard (*) does not work in the [System.IO.File]::Exists, the example is given below for reference.

[System.IO.File]::Exists("C:\Users\powershell\Test\*.doc")

As we already know that there is a file5.doc in the Test folder but the System.IO.File]::Exists ignored the wildcard and returned the output as False, which means there is no “.doc” file in the particular folder.

Great! You have learned to check if the file exists or not.

Conclusion

PowerShell can be used to check if the file of the particular path exists or not. For this purpose, Powershell provides four built-in methods including Test-Path, Get-ChildItem, Get-Item, and [System.IO.File]::Exists to check the availability of file. In this article, all these methods are discussed and explained briefly to check the existence of the file.

About the author

Adnan Shabbir