Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

iTechGuides is reader-supported. When you buy through links on our site, we may earn an affiliate commission. As an Amazon Associate I earn from qualifying purchases. Learn more

When you write PowerShell scripts, you may encounter multiple scenarios when you need to check if a file or a folder exists. In this guide, I teach you various ways to use the Test-Path command to check if an item exists.

Option 1: Check if a File Exists without Using a Variable

This section teaches you how to run Test-Path command without a variable.

You can run Test-Path as a stand-alone command. Moreover, you can also combine the command with an IF statement to perform additional tasks based on the result of the Test-Path command.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Example 1: Run Test-Path Directly Without a Variable

This sub-section teaches you how to run Test-Path alone (without using the result in an IF statement).

Popular on Amazon
Seagate 2TB Portable Hard Drive | USB 3.0 (STGX2000400) | Everyday HDD storage. Plug-and-play backup. Windows & macOS. Adobe CC trial included. 1-Year Warranty
  • Easily store and access 2TB to content on the go with the Seagate Portable Drive, a USB external hard drive
  • Designed to work with Windows or Mac computers, this external hard drive makes backup a snap just drag and drop
  • To get set up, connect the portable hard drive to a computer for automatic recognition no software required
  • This USB drive provides plug and play simplicity with the included 18 inch USB 3.0 cable
  • The available storage capacity may vary.

I have a file called processes.txt in E:\reports.

How to Use PowerShell to Check if a File Exists

To check if the file exists, enter the command below in PowerShell and press enter

Test-Path -Path E:\reports\processes.txt

When I pressed, PowerShell returned True, meaning that the file exists.

How to Use PowerShell to Check if a File Exists

However, if I run the same command for a file that does not exists, PowerShell returns False.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
How to Use PowerShell to Check if a File Exists

Example 2: Combine Test-Path and IF Statement without a Variable

As I mentioned earlier, after using PowerShell Test-Path to check if a file exists, you can use the result in an IF statement to perform additions tasks.

In this example, I will check if “E:\reports\processes.txt” exists and if it exists, I will copy the file to “C:\reports”. Here is the script that will do the job.

It is recommended to run this command in PowerShell ISE
If (Test-Path -Path E:\reports\processes.txt ) {
Copy-Item -Path E:\reports\processes.txt -Destination C:\reports
}

Here is the result in PowerShell ISE and the file copied in C:\reports

How to Use PowerShell to Check if a File Exists

I can make the script smarter by adding two additional lines.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Write-Host "file copied"

and….

Else {
Write-Host "file does not exist"
}
The first command above displays a message if the file is copied. The second displays a message that the file does not exist.

Here is the modified script…

If (Test-Path -Path E:\reports\processes.txt ) {
Copy-Item -Path E:\reports\processes.txt -Destination C:\reports
Write-Host "file copied"
}
Else {
Write-Host "file does not exist"
}

Option 2: Check if a File Exists Using a Variable

In the last section I showed you how to use PowerShell to check if a file exists without using variables. In this section, I will save the result of Test-Path in a variable. Then, use the variable in an IF statement.

In the example in this section, I want to check if first-file.txt exists in the path shown in the image below. Then, if it exists, I will copy the file to “C:\PS”

How to Use PowerShell to Check If a File Exists with a Variable

In the first steps, I will run the command below:

$fileexists = Test-Path -Path E:\reports\first-file.txt

This command is the same as the last one with one difference – the result of the command is saved in the fileexists variable.

How to Use PowerShell to Check If a File Exists with a Variable

Example 3: Check if a File Exists and Copy it

When I introduced this section, I saved the output of the Test-Path command to the fileexists variable.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
$fileexists = Test-Path -Path E:\reports\first-file.txt

In this subsection, I will teach you how to copy the file if it exists. To do this, I will introduce the IF statement with the Copy-Item command.

If ($fileexists ) {
Copy-Item -Path E:\reports\first-file.txt -Destination C:\PS
}

Here is the full script with the combined commands:

$fileexists = Test-Path -Path E:\reports\first-file.txt
If ($fileexists ) {
Copy-Item -Path E:\reports\first-file.txt -Destination C:\PS
}

And here is the script in PowerShell ISE.

Example 4: Check if a File Exists and Delete it

In the last sub-section, you saw how to check if a file exists and copy the file. What if you want to copy the file instead?

Also popular on Amazon
Seagate Portable 5TB External Hard Drive HDD – USB 3.0 for PC, Mac, PS4, & Xbox - 1-Year Rescue Service (STGX5000400), Black
  • Easily store and access 5TB of content on the go with the Seagate portable drive, a USB external hard Drive
  • Designed to work with Windows or Mac computers, this external hard drive makes backup a snap just drag and drop
  • To get set up, connect the portable hard drive to a computer for automatic recognition software required
  • This USB drive provides plug and play simplicity with the included 18 inch USB 3.0 cable
  • The available storage capacity may vary.

If you want to delete the file instead of copying it, replace the Copy-Item command with the Remove-Item command.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Here is the updated script that uses PowerShell “IF” statement to check if a file exists. Then, if it exists, delete it…

$fileexists = Test-Path -Path E:\reports\first-file.txt
 If ($fileexists ) {
 Remove-Item -Path E:\reports\first-file.txt -Force
 }

Example 5: Check if a File Exists Else Exit

In some PowerShell scripting situations, you may want to check if a file exists. Then, if it exists, perform a specified task – like copy or delete the file – otherwise, exit the script.

To achieve this, all you need to do is add the Else block of the If statement.

In the last sub-section, I used this script to delete a file if it exists.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
$fileexists = Test-Path -Path E:\reports\first-file.txt
If ($fileexists ) {
Remove-Item -Path E:\reports\first-file.txt -Force
}

If I want the script to exit if the file does not exist, I will modify the script as shown below…

$fileexists = Test-Path -Path E:\reports\first-file.txt
 If ($fileexists ) {
 Remove-Item -Path E:\reports\first-file.txt -Force
 }
Else {
Break
}

Here is the logic of the modified script…

Check if the file, E:\reports\first-file.txt exists. If it exists, delete the file.

However, if the file does not exist, run the command in the Else block of the If statement. In this instance, the command in the Else loop is the word, Break.

“Break” tells PowerShell to stop processing the script. You may also add Break on the “If” block of the statement – beneath the Remove-Item command…

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
$fileexists = Test-Path -Path E:\reports\first-file.txt
 If ($fileexists ) {
 Remove-Item -Path E:\reports\first-file.txt -Force
 Break
 }

Example 6: Check if a File Exists and Not Empty

This is yet another condition common in PowerShell scripting – checking if a file exists and not empty. This may be applicable for log files.

For example, if you have an application that creates and updates log files, you may write a PowerShell script that checks that new log files are updated.

Moving on, to test if a file exists, you use the Test-Path command. However, to check if the file is empty, you need to introduce another command, Get-ChildItem.

In this scenario, you can use the Get-ChildItem command in two ways.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

1, if you want to check if a single file is empty, you can run Get-ChildItem command, using the full file path as the Path parameter. 2, alternatively, if you want to check a bunch of files in a folder, use the folder path as the Path parameter.

Also popular on Amazon
Seagate Portable 1TB External Hard Drive HDD – USB 3.0 for PC, Mac, PlayStation, & Xbox, 1-Year Rescue Service (STGX1000400) , Black
  • Easily store and access 1TB to content on the go with the Seagate Portable Drive, a USB external hard drive.Specific uses: Personal
  • Designed to work with Windows or Mac computers, this external hard drive makes backup a snap just drag and drop. Reformatting may be required for Mac
  • To get set up, connect the portable hard drive to a computer for automatic recognition no software required
  • This USB drive provides plug and play simplicity with the included 18 inch USB 3.0 cable
  • The available storage capacity may vary.

In this section, I will give an example that checks if a specific file exists and is empty. I will cover the second option in the How To Use PowerShell To Check If A File Exists An A Folder section later in this guide.

In this example, I want to check if the file, first-file.txt and if it is empty.

As I mentioned earlier, the first step is to run the Get-ChildItem command, using the full file path as the Path parameter. To begin, run the command below…

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Get-ChildItem -Path E:\reports\first-file.txt

Here is the result of the command. From the result of the command, you can see that the file has a property called Length.

Effectively, the Length property of a file shows the size of the file. Moreover, if a file is empty, the Length will be zero (0).

So, to use PowerShell to check if is Not empty, we will check if the Length is NOT zero (0).

To achieve this, I will modify the last command as shown below…

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
(Get-ChildItem -Path E:\reports\first-file.txt).Length -ne "0"

Here is the result of the command in PowerShell. In this instance, because the size of my file is 0 (empty), the command returned false.

This means that this file is empty.

But how do we include the bit that tests if the file exists? Glad you asked!

To introduce the logic that uses PowerShell to checks if a file exists and also check that the file is NOT empty, I will combine parts of our previous script and the Get-ChildItem command above.

Here is my modified script…

$fileexists = Test-Path -Path E:\reports\first-file.txt
$checkiffileisempty = (Get-ChildItem -Path E:\reports\first-file.txt).Length -ne "0"
 If (($fileexists -eq $True) -and ($checkiffileisempty -eq $False)) {
Set-Content -Path E:\reports\first-file.txt -Value "This will add some text for this file"
 }

In this modified version of the script, I made the following changes…

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
  1. In line 2, I saved the Get-ChildItem command that checks if a file is empty – in a variable called $checkiffileisempty.
$checkiffileisempty = (Get-ChildItem -Path E:\reports\first-file.txt).Length -ne "0"
  1. Then, in line 3 I added another condition to the If statement. The condition part of the If the statement was updated to “If (($fileexists -eq $True) -and ($checkiffileisempty -eq $False))”.

    This condition tests if what is saved in $fileexists is True (file exists). Furthermore, it checks if what is saved in $checkiffileisempt is False (file is empty).

    Effectively, PowerShell checks if the Test-Path command that checked if the file path exists is True. Additionally, it also checks if the Get-ChildItem command that checks if the file is NOT empty is False (meaning that the file is empty)
  2. Finally, if both conditions are met, in line 4, PowerShell will execute the command in the {} bracket of the “If” statement.

    In this example, I have this command in the {} bracket of the “If” statement.
Set-Content -Path E:\reports\first-file.txt -Value "This will add some text for this file"

The command adds the text in the Value parameter of Set-Content command.

To test that the script works, copy it to PowerShell ISE and run the script…

Once I ran the script, the file, first-file.txt – that was previously empty – now has some values in it. Additionally, the file size is no longer zero (0)!

Also popular on Amazon
Seagate Portable 4TB External Hard Drive HDD – USB 3.0, 1-Year Rescue | Everyday HDD storage. Plug-and-play backup. Windows & macOS. Adobe CC trial included. 1-Year Warranty
  • Easily store and access 4TB of content on the go with the Seagate Portable Drive, a USB external hard drive.Specific uses: Personal
  • Designed to work with Windows or Mac computers, this external hard drive makes backup a snap just drag and drop
  • To get set up, connect the portable hard drive to a computer for automatic recognition no software required
  • This USB drive provides plug and play simplicity with the included 18 inch USB 3.0 cable
  • The available storage capacity may vary.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Option 3: Check if a File Exists in a Folder

So far this guide has covered multiple scenarios to use PowerShell to check if a file exists.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

In this section, I want to dive deep into folders. Specifically, you will learn how to check if a file exists in the current folder or in all subfolders.

Additionally, you will learn how to use PowerShell to check if a file exists in multiple folders. Finally, I will show you how to use a wildcard to check if a file exists.

Example 7: Check if a File Exists in the Current Folder

In this first example, you will learn how to check if a file exists in the current folder – excluding all subfolders.

In this example, I want to check if files with the “.log” file extension exist in the “E:\reports” folder. However, I want to exclude all subfolders.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

The first step is to run the Get-ChildItem command. Then, pipe the output to Where-Object and filter by PSIsContainer property.

The PSIsContainer property is a property that defines folders or directories. In the Where-Object command, I specified “$_.PSIsContainer -ne $true”. This command simply says “Do not return any item if its PSIsContainer property is True. This will return only files.

Here is the command…

Get-ChildItem E:\reports | Where-Object {$_.PSIsContainer -ne $true}

The final step is to return the full path of all files with the “.log” extension in the current folder.

Here is the command…

Get-ChildItem E:\reports | Where-Object { ($_.PSIsContainer -ne $true) -and ($_.Extension -eq ".log")}  | Select-Object FullName
From this point, if you want to perform further tasks on the files, you can pipe the output of the last result into a ForEach loop. Then, within the ForEach loop, you can run additional commands.

Example 8: Check if a File Exists in Subfolders

If you want to check if a specific file or files exists in all subfolders, not on the current folder, you need to make slight changes to the Get-ChildItem command.

Specifically, when you pipe the Get-ChildItem command, into Where-Object, you filter Where-Object to return only folders.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
(Get-ChildItem E:\reports | Where-Object {$_.PSIsContainer}).FullName

The command returns the full path to all the subfolders in the specified folder…

In the next step, you need to use ForEach statement to iterate through all the folders returned by the last command. Then, within the ForEach statement, run another Get-ChildItem command against each subfolder.

There are three variants of ForEach but, in this example, I will use the ForEach-Object Cmdlet.

(Get-ChildItem E:\reports | Where-Object {$_.PSIsContainer}).FullName | ForEach-Object {
     Get-ChildItem $_ | Where-Object { ($_.Extension -eq ".log")}  | Select-Object Directory, FullName
}

The command displays all log files in the subfolders…

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Example 9: Check if a File Exists in the Root and Subfolders

In the last 2 sub-sections, I used the PSIsContainer property to either filter out subfolders, or return only subfolders. In this example, I will eliminate the PSIsContainer property.

Also popular on Amazon
Sale
UnionSine 500GB Ultra Slim Portable External Hard Drive HDD-USB 3.0 | for PC, Mac, Laptop, PS4, Xbox one, Xbox 360-(Black)
  • [Upgraded Version] - This external hard drive features a mirrored logo stripe combined with a striped anti-slip design, and the rounded corners of the casing make it easier to grip. The stripes also have a heat dissipation function, ensuring stable and fast data transfer.
  • 【Ultra-thin and quiet】 - The motherboard adopts JMicron 578 noise-free solution, giving you a quiet working environment. Lightweight and portable size designed to fit in your pocket for easy portability.
  • 【Ultra-Fast Data Transfers】 - Pairing this external hard drive with JMicron 578 solution USB 3.0 and USB 2.0 interfaces enables blazing-fast data transfer. It boasts theoretical read speeds of up to 125MB/s and write speeds of up to 103MB/s.
  • 【Plug and Play】 - With no software to install, just plug it in and the drive is ready to use.The hard disk chip is wrapped with an aluminum anti-interference layer to increase heat dissipation and protect data.
  • 【What You Get】 - 1 x Portable Hard Drive, 1 x USB 3.0 Cable, 1 x User Manual, Gift-type shell packaging ,Three-year manufacturer's warranty and free technical support services.

However, I will introduce a Get-ChildItem command parameter, called Recurse. The Recurse parameter forces Get-ChildItem to check the specified folder and all its subfolders.

Here is modified script.

(Get-ChildItem E:\reports -Recurse).FullName | ForEach-Object {
     Get-ChildItem $_ | Where-Object { ($_.Extension -eq ".log")}  | Select-Object Directory, FullName
}

The Recurse parameter is introduced in the first Get-ChildItem command. It is not required in the second Get-ChildItem command (within the ForEach-Object block).

Here is the result of the command. This time, the report includes all log files in the top folder and all its subfolders.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Option 4: Check if a File Exists in a Remote Computer

If you want to test if a file exists on a remote computer, you can apply the same principle discussed in all the examples in this guide.

However, you need to introduce a new command called Invoke-Command. Then, within the Invoke-Command command, use its ScriptBlock parameter to specify one of the commands we discussed earlier in this guide.

Another important thing to note is that, you may need to specify a user name and password for an account with the permission to connect to the remote computer.

Additionally, the specified account should have the permission to run the command in the ScriptBlock parameter – on the remote computer.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Finally, use the ComputerName parameter to specify the name of the remote computer.

For your reference, here is the general syntax of the Invoke-Command command.

Invoke-Command -ComputerName -Credential -ScriptBlock {Test-Path}

To read more about the Invoke-Command command, see “References and Further Reading” at the end of this guide.

Conclusion

The primary command you require to test if an item exists using PowerShell is Test-Path. However, I have demonstrated different ways to combine other conditions with this command-let to achieve different results.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Let me know your thoughts about this article by responding to the “Was this page helpful?” question below.

Finally, for more Email tech Itechguides, visit our Windows PowerShell how-to page. You may also find our Work from Home page very helpful.

Quick Recap

Bestseller No. 1
Seagate 2TB Portable Hard Drive | USB 3.0 (STGX2000400) | Everyday HDD storage. Plug-and-play backup. Windows & macOS. Adobe CC trial included. 1-Year Warranty
Seagate 2TB Portable Hard Drive | USB 3.0 (STGX2000400) | Everyday HDD storage. Plug-and-play backup. Windows & macOS. Adobe CC trial included. 1-Year Warranty
This USB drive provides plug and play simplicity with the included 18 inch USB 3.0 cable; The available storage capacity may vary.
$145.69
Bestseller No. 2
Seagate Portable 5TB External Hard Drive HDD – USB 3.0 for PC, Mac, PS4, & Xbox - 1-Year Rescue Service (STGX5000400), Black
Seagate Portable 5TB External Hard Drive HDD – USB 3.0 for PC, Mac, PS4, & Xbox - 1-Year Rescue Service (STGX5000400), Black
This USB drive provides plug and play simplicity with the included 18 inch USB 3.0 cable; The available storage capacity may vary.
$216.99
Bestseller No. 3
Seagate Portable 1TB External Hard Drive HDD – USB 3.0 for PC, Mac, PlayStation, & Xbox, 1-Year Rescue Service (STGX1000400) , Black
Seagate Portable 1TB External Hard Drive HDD – USB 3.0 for PC, Mac, PlayStation, & Xbox, 1-Year Rescue Service (STGX1000400) , Black
This USB drive provides plug and play simplicity with the included 18 inch USB 3.0 cable; The available storage capacity may vary.
$119.80
Bestseller No. 4
Seagate Portable 4TB External Hard Drive HDD – USB 3.0, 1-Year Rescue | Everyday HDD storage. Plug-and-play backup. Windows & macOS. Adobe CC trial included. 1-Year Warranty
Seagate Portable 4TB External Hard Drive HDD – USB 3.0, 1-Year Rescue | Everyday HDD storage. Plug-and-play backup. Windows & macOS. Adobe CC trial included. 1-Year Warranty
This USB drive provides plug and play simplicity with the included 18 inch USB 3.0 cable; The available storage capacity may vary.
$189.99
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply. Prices as of November 7, 2026 7:58 am GMT.