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
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
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(“
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
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
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