Like any other shell, PowerShell is a powerful shell and scripting language developed by Microsoft. Originally designed for Windows, PowerShell is now available for all the major platforms. Starting from PowerShell 6, it’s become a free and open-source application.
Similar to any other shell scripting, we can use PowerShell to automate from basic to advanced stuff. In this guide, check out how to use PowerShell to delete a file if it exists on Linux.
Checking If a File Exists
Similar to other shells, PowerShell comes with its own set of commands to perform various tasks, known as cmdlets. To test a certain statement, in bash, we’d use the bash test statement that checks whether a certain argument is true or false. The test statement is a general-purpose mechanism that can calculate a wide variety of possible commands.
In the case of PowerShell, however, there are specific cmdlets for a specific task. Here, we’ll be needing Test-Path to check if the given file exists in the defined location.
Here’s a simple demonstration of Test-Path in action.
By default, Test-Path will check if the specified element exists in the location described. If it exists, then it returns the Boolean value “true”. Otherwise, it returns “false”.
Deleting a File in PowerShell
In Linux, we’d use the rm command to delete a file or directory. In the case of PowerShell, it uses the cmdlet Remove-Item.
Check out the Microsoft documentation on Remove-Item.
For example, to remove a specific file, run the following command.
To remove a directory, we need an additional option “-Recurse”.
Deleting File(s) If Exists
Now, we can combine both of the methods together and make a simple script that will delete a file if it exists in the target location.
First, assign a variable that will hold the value of the file path.
Next, use Test-Path to verify if the file exists in the location.
Depending on the return value of Test-Path, we can make a decision to remove the target file. We can use the PowerShell if-else statement to determine the action.
Remove-Item $FileName
$ }
Let’s add some additional messages for each possible outcome.
Remove-Item $FileLocation
Write-Host "$FileLocation is removed"
}
else {
Write-Host "$FileLocation does not exist"
}
Implementing into a PowerShell Script
It’s time to put all the codes into a suitable PowerShell script. Similar to bash scripts, we can use PowerShell scripts to automate various tasks using PowerShell cmdlets and commands. On Linux, both PowerShell and Bash scripts share structural similarities.
First, we need the location of the PowerShell executable location. It’s important as it will be a part of the shebang. Run the following command.
We can now put all these pieces together into a single PowerShell script. A PowerShell script comes with the file extension ps1.
Now, enter the code we’ve developed so far.
if (Test-Path $FileLocation) {
Remove-Item $FileLocation
Write-Host "$FileLocation is removed"
}
else {
Write-Host "$FileLocation does not exist"
}
Save the file and close the editor. Mark the file as an executable.
Finally, execute the script.
Special Situations
Deleting read-only files
The Remove-Item cmdlet can’t delete non-existent or read-only files. In such a situation, we can change the Remove-Item command to delete, irrespective of file permission. The flag “-Verbose” will print out step-by-step output for easier debugging.
Let’s update it in the script.
if (Test-Path $FileLocation) {
Remove-Item -Verbose -Force $FileLocation
Write-Host "$FileLocation is removed"
}
else {
Write-Host "$FileLocation does not exist"
}
Final Thought
This guide showcases how to check the existence of a file and based on the result, how to delete it using Remove-Item cmdlet. If the file is read-only or permission denied, then we can use the “-Force” parameter to delete it forcefully. Finally, we implemented the entire script.
This is a very simple yet effective way of removing a file/directory of choice. However, as demonstrated, this overall method is capable of removing only a single item. It’s possible to get the list of files from a directory and use a loop to delete them incrementally.
Happy computing!