Although checking and removing files is relatively easy using the Windows GUI, you still have to navigate to multiple windows just to delete a single file. On the other hand, Powershell lets you do the same task using simple and short commands.Well, you can simply use the Remove-Item cmdlet to delete a specific file from the given path. However, there are multiple commands and a plethora of methods to check whether the item you’re trying to delete exists or not.Regarding the same, this article will guide you with the various commands and useful techniques to check and delete the existing files in Windows Powershell.

How to Check and Delete an Existing File in Powershell?

Windows Powershell is a terminal that lets users automate tasks using scripting languages. Thus, creating, reading, updating, or deleting files using this utility is easy to understand and implement.Probably, you have tried checking if a file exists in Bash, Command Prompt, and Windows GUI. Now, in this section, you will learn different commands to do it in Powershell and delete it if required.

Using Test-Path

Test-Path is a command that determines whether the provided path/address exists. Basically, running this cmdlet on Powershell outputs a boolean expression. Here, True indicates that the file you’re trying to access exists, and False means it doesn’t. Below is the syntax for Test-Path:Test-Path For example, to check whether the file “text.txt” exists inside the Documents directory and delete it, here’s what you can do:If you do not prefer the boolean expressions, you can use the if and else statements to generate actual messages. Also, writing this simple code will help you delete the existing file without having to run multiple commands:In the above code, we are basically representing the location by the variable MyFile. Now, the if (Test-Path $MyFile) determines whether the path exists or not. If it does, you’ll encounter the message “The file was found and has been deleted successfully.” and the Remove-Item $MyFile command deletes your file.To ensure this, you can use the Test-Path Documents\text.txt again, and you should get the False output.However, if the file doesn’t exist, you’ll simply encounter the “The file you’re searching for doesn’t exist.” message instead of the Cannot find path error as earlier.

Using Get-Item

Using Get-Item is as simple as using the Test-Path. However, this cmdlet doesn’t determine if a file exists using boolean expressions. Instead, it outputs your file’s mode, last write time, length, and name, along with its type. Given below is the syntax of Get-Item:Get-Item Please follow the instructions below to learn how to identify and delete an existing file using the Get-Item cmdlet. For demonstration purposes, we will remove an image file “image.bmp” from the Documents directory:Well, you can also use the below program to check and delete an existing file in Powershell using the Get-Item:In the above script, we have first assigned a variable MyFile that stores the path of the image.bmp file. Then, we set the if condition as Get-Item $MyFile -ErrorAction Ignore, where Get-Item $MyFile gets the item from the location, and –ErrorAction Ignore will discard the error if the file doesn’t exist.Basically, if your file exists, you’ll get the “The file was found and has been deleted successfully” message, and the item is deleted.Otherwise, you’ll encounter “The file you’re searching for doesn’t exist and can’t be deleted.”

Using [System.IO.File]::Exists

System.IO deals with reading and writing into the files in the .NET framework class library. Using the namespace, we can quickly determine if a file exists. Then, using the Remove-Item cmdlet, deletion is also possible. Like Test-Path, the [System.IO.File]::Exists command also throws a boolean expression to determine whether a file exists or not. Here’s the syntax of the [System.IO.File]::Exists cmdlet:[System.IO.File]::Exists(“”)For demonstration, let’s check if an MS Word file ‘word.docx’ exists in the Document directory and delete this item:Furthermore, you can try the below program that adopts the [System.IO.File]::Exists cmdlet to delete an existing file on Windows Powershell:In the above program, MyFile is a variable that stores the path of the MS Word file. Now, using the [System.IO.File]::Exists($MyFile), we get to know whether the item exists or not.Now, instead of displaying the boolean expression, the Write-Host cmdlet displays “The file was found and has been deleted successfully.” and deletes the file if it exists.Similarly, you get the message, “The file you’re searching for doesn’t exist and can’t be deleted.” if the path isn’t set right.

How to Delete Multiple Existing Files in Powershell?

Sometimes, you might want to delete multiple files from a folder or directory. In such a case, Windows Powershell provides a feature to remove all or selected files permanently. In this section, we will discuss some easy and effective ways to check and delete multiple existing files in the Powershell program.

All Files From a Specific Folder

If you wish to delete all the files from a particular folder regardless of the file type or extension, here’s the appropriate command you can use:Remove-Item *.*For example, let’s check the files inside the Example folder and delete all the files inside it. Here’s how you can do it:

Hidden or Read-Only Files

Well, removing all the files won’t delete those with read-only and hidden attributes. In fact, if you try removing such items, you’ll get the You do not have sufficient access rights to perform this operation error.Hence, you need to use the -Force parameter to delete them forcefully:Remove-Item -ForceLet’s take an example to understand this. Suppose we have a hidden and read-only file, ‘word.docx’, inside the Example folder. So, to remove this, here’s the correct command you need to use:Remove-Item C:\Users\Bhishu\Desktop\Example\word.docx -ForceNow, you should not get the error. In fact, you can validate this by using the Test-Path command to check whether the hidden file now exists in the same location.

All Files of the Same Type

If you do not want to delete all the files from a specific folder but rather wish to remove only those items having the same file type, here’s the syntax you’re looking for:Remove-Item *[file-type]For example, to delete all the files having the .docx extension from the Example folder, please follow the below steps: How To Check And Delete Existing File In Powershell - 37How To Check And Delete Existing File In Powershell - 78How To Check And Delete Existing File In Powershell - 67How To Check And Delete Existing File In Powershell - 73How To Check And Delete Existing File In Powershell - 18How To Check And Delete Existing File In Powershell - 87How To Check And Delete Existing File In Powershell - 1How To Check And Delete Existing File In Powershell - 88How To Check And Delete Existing File In Powershell - 31How To Check And Delete Existing File In Powershell - 81How To Check And Delete Existing File In Powershell - 3How To Check And Delete Existing File In Powershell - 67How To Check And Delete Existing File In Powershell - 55How To Check And Delete Existing File In Powershell - 39How To Check And Delete Existing File In Powershell - 90How To Check And Delete Existing File In Powershell - 37How To Check And Delete Existing File In Powershell - 11How To Check And Delete Existing File In Powershell - 1How To Check And Delete Existing File In Powershell - 88How To Check And Delete Existing File In Powershell - 79How To Check And Delete Existing File In Powershell - 71How To Check And Delete Existing File In Powershell - 55How To Check And Delete Existing File In Powershell - 32How To Check And Delete Existing File In Powershell - 1How To Check And Delete Existing File In Powershell - 46