Home » PowerShell Tips » PowerShell – Delete File If Exists

PowerShell – Delete File If Exists

PowerShell has Remove-Item cmdlet used to delete one or more items. These items can be files, folders, variables, registry keys, functions, and aliases. Using PowerShell Remove-Item cmdlet, we can test if file exists or not and delete file if exists.

In this article, I will explain how to use PowerShell Remove-Item to delete file if exists.

PowerShell Delete File If Exists

PowerShell Remove-Item cmdlet delete file using specified file path, run below PowerShell script

$FileName = "D:\PowerShell\ZeroFileSize.txt"
if (Test-Path $FileName) {
  Remove-Item $FileName
}

In the above PowerShell script, the $FileName variable contains a file path.

PowerShell Test-Path cmdlet check if file exists or not. If file exists, it will return $True and $False if the file doesn’t exist on a specified path.

PowerShell Remove-Item cmdlet is used to delete the file if exists from the specified path by the $FileName variable.

Above command, remove zeroFileSize.txt file as it exists on the specified directory.

Note: Using the above PowerShell script, you can remove txt file if exists even if it opened. However, if you try to use a script to delete excel or CSV file while the file is opened, it will throw an error as “The process cannot access the file, because it is being used by another process.”

Cool Tip: how to count files in folder using Get-ChildItem in PowerShell!

PowerShell – Delete Read only File If Exists

PowerShell Remove-Item cmdlet is used to delete file if exists using the filename path specified.

However, if the file is having read-only permission or file access is denied, if we use the below command to delete a file

$FileName = "D:\PowerShell\File-Delete.txt"
if (Test-Path $FileName) {
  Remove-Item $FileName
}

above PowerShell script will throw error as

Remove-Item : Cannot remove item D:\PowerShell\File-Delete.txt: You do not have sufficient access rights to perform this 
operation.
At line:3 char:3
+   Remove-Item $FileName
+   ~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : PermissionDenied: (D:\PowerShell\File-Delete.txt:FileInfo) [Remove-Item], IOException
    + FullyQualifiedErrorId : RemoveFileSystemItemUnAuthorizedAccess,Microsoft.PowerShell.Commands.RemoveItemCommand

We need to use -Force parameter to forcefully delete file. Run below command,

$FileName = "D:\PowerShell\File-Delete.txt"
if (Test-Path $FileName) {
  Remove-Item -Verbose -Force $FileName
}

In the above PowerShell script, we use -Verbose parameter to print Remove-Item operation message and using Force parameter to forcefully delete file if exists.

Cool Tip: Find Large size files in PowerShell!

Conclusion

I hope above article on how to delete file if exists in PowerShell using Remove-Item cmdlet helpful to you.

To delete a read-only file or access permission denied file, use Force parameter to forcefully remove file from a specified location.

You can find out here about how to delete folder if exists in PowerShell.

You can find more topics about PowerShell Active Directory commands and PowerShell basics on the ShellGeek home page.