PowerShell: How to Delete File if it Exists


You can use the following methods in PowerShell to delete a file only if it exists:

Method 1: Delete One File if it Exists

$file_name = "this_data.txt"

if (Test-Path $file_name) {
   Remove-Item $file_name -verbose
} else {
    Write-Host "File does not exist"
}

This particular example deletes the file named this_data.txt only if it exists in the current directory. If it does not exist, then the message “File does not exist” is shown.

Method 2: Delete Multiple Files if they Exist

$file_names = "this_data.txt", "that_data.txt", "my_data.txt"
foreach($name in $file_names)
{
    if (Test-Path $name) {
       Remove-Item $name -verbose
    } else {
        Write-Host "File does not exist"
    }
}

This particular example deletes the files named this_data.txt, that_data.txt and my_data.txt only if they exist in the current directory.

For each file, this syntax either deletes the file or displays a message stating that the file does not exist.

The following examples show how to use each method in practice.

Example 1: Delete One File if it Exists Using PowerShell

Suppose that our current directory has the following path:

  • C:\Users\bobbi\OneDrive\Documents\test_data

We can type Dir to list out all files in this current directory:

We can see that there are three files in the directory:

  • my_data.txt
  • my_new_data.txt
  • some_old_data.txt

Suppose that we would like to attempt to delete a file named this_data.txt only if it exists.

We can use the following syntax to do so:

$file_name = "this_data.txt"

if (Test-Path $file_name) {
   Remove-Item $file_name -verbose
} else {
    Write-Host "File does not exist"
}

The following screenshot shows how to use this syntax in practice:

PowerShell delete file if it exists

Since this file doesn’t exist, we receive a message that says “File does not exist.”

Suppose instead that we attempt to delete a file named my_data.txt:

Since this file does exist, we are able to successfully delete it.

And when we run the Dir command again, we can see that the file no longer exists in the directory.

Example 2: Delete Multiple Files if they Exist Using PowerShell

Suppose that our current directory has the following path:

  • C:\Users\bobbi\OneDrive\Documents\test_data

We can type Dir to list out all files in this current directory:

Suppose that we would like to attempt to delete the following files from the directory:

  • this_data.txt
  • that_data.txt
  • my_data.txt

We can use the following syntax to do so:

$file_names = "this_data.txt", "that_data.txt", "my_data.txt"
foreach($name in $file_names)
{
    if (Test-Path $name) {
       Remove-Item $name -verbose
    } else {
        Write-Host "File does not exist"
    }
}

The following screenshot shows how to use this syntax in practice:

PowerShell delete multiple files if they exist

From the output we can see:

  • this_data.txt does not exist, so we receive the message “File does not exist.”
  • that_data.txt does not exist, so we receive the message “File does not exist.”
  • my_data.txt does exist, so it is deleted.

We then ran the Dir command once again to confirm that my_data.txt was deleted from the directory while all other files were left untouched.

Related Tutorials

The following tutorials explain how to perform other common tasks in PowerShell:

PowerShell: How to Delete All Files with Specific Extension
PowerShell: How to Replace Every Occurrence of String in File
PowerShell: How to Replace Text with Wildcard
PowerShell: How to Replace Special Characters in String

Leave a Reply