Using Powershell to delete files is an efficient method to perform disk space maintenance. Because with PowerShell, the housekeeping task is able to be automated using scripts. The logic for filtering or error handling is able to be added for a more controlled process of deleting files. In this article, you are going to learn how to make use of the WMI delete method and the Remove-Item cmdlet to delete files by using Powershell.
DELETING FILES USING WMI IN POWERSHELL
Powershell comes with support for WMI. It means that WMI queries and methods can be called from within Powershell. Yeah, WMI is not only for Visual Basic scripts that admins used in the earlier days of Windows. The WMI specific CIM cmdlets were released in Powershell 3.0. The CIM cmdlets which will be used to delete files are the ‘Get-CimInstance’ and ‘Invoke-CimMethod’.
Method 1: Using Powershell to Delete a File (WMI)
This method assumes that you know the path of the specific file to delete. The ‘Get-CimInstance’ cmdlet with the ‘Cim_DataFile’ class is used to resume the information regarding the file to delete which is C:\Temp\random.txt. In the code, the filter parameter receives a WQL format query. Using WQL needs that some characters are escaped including the backslash. As the WQL escape character is also the backslash, resulting in the double-backslash characters – \\.
The information about C:\Temp\random.txt is saved to the $file2delete variable. Now that the information for the file C:\Temp\random.txt is regain, the resulting object in the $file2delete variable is able to be piped to the ‘Invoke-CimMethod’ cmdlet. The ‘Invoke-CimMethod’ cmdlet has a parameter named -Name, which represents the name of the method of the ‘Cim_DataFile’ class.
$file2delete | Invoke-CimMethod -Name Delete
The result of the command above is shown. As you see in the screen, the ReturnValue shows 0, it means that the command was successful. In addition, the screenshot shows that after running the ‘Invoke-CimMethod’ Delete method, only the C:\Temp\random.txt file was deleted. While all the other files are left untouched.
Method 2: Using Powershell to Delete All Files in Folder (WMI)
In this next method, we are going to show you how to delete all files in a folder by using Powershell and WMI. Similar cmdlets used in the previous example, that are ‘Get-CimInstance’ and ‘Invoke-CimMethod’. However, now the WQL query will regain all files in the folder instead of just one specific file.
In the next code, the ‘Get-CimInstance’ cmdlet regains all files which are located in C:\temp. As you can see, the query filters the Drive and Path properties of the Cim_DataFile class. Running the code will save the retrieved information about the files in C:\temp in the $file2delete variable. Seeing the value(s) of the $file2delete variable shows the output. Now, the values stored in the $file2delete variable are able to be piped to the ‘Invoke-CimMethod’ to delete all files in c:\temp. After running the command in Powershell, the result is shown. Please remember that the ReturnValue code of 0 means successful, and for each file that the delete method was named on will have its own ReturnValue code.
Method 3: Using Powershell to Delete Files By Extension (WMI)
You have seen in the previous method e how to delete all files in a folder regardless of extension. But, control over which files get deleted based on extension can also be achieved. You are going to notice in the code. this time the query has an added condition. This added condition means that only files which match the .LOG extension is returned. The percent sign is a WQL LIKE operator that means a string of zero or more characters. In programming terms, the percent is the equivalent of a wildcard, which is the asterisk character. The demonstration shows that before the code is executed, there are nine *.LOG files and only one *.TXT file. When the code is done running, the *.LOG files are removed and the *.TXT file is the only file left in the folder.
GET-CHILDITEM AND REMOVE-ITEM: THE POWERSHELL WAY TO DELETE FILES
As explained at the beginning of this article, there are two ways to delete files in Powershell that you are going to learn here. You have already gone through the first one which is using the WMI delete method by using the CIM cmdlets ‘Get-CimInstance’ and ‘Invoke-CimMethod.’
In this part, you are going to learn about the cmdlet which is specifically for removing items in Powershell, the Remove-Item cmdlet. Another cmdlet which works hand in hand with Remove-Item is the Get-ChildItem cmdlet, which is used to regain the list of files and folders.
Method 1: Using Powershell to Delete a File
The first method that will be most useful is the most basic that is, deleting a single file. To delete just a single file, you only need to use the command: Remove-Item -Path C:\temp\random.txt. The code deletes the file C:\temp\random.txt. Running the code in Powershell will not show anything on the screen unless an error was found.
Method 2: Using Powershell to Delete All Files in Folder
In this method, the code will remove all files in a folder. The ‘Get-ChildItem’ cmdlet targets C:\temp with the -Path parameter. The -Fileparameter indicates that the only type of the item to be included are files. While folders are ignored. As explained, the code removes all files in the C:\temp folder.
Method 3: Using Powershell to Delete All Files Recursively and Dealing with the Long Path Problem
In the previous method, only the files in the directory C:\temp are removed. If you also need to delete the files inside every sub-directory, you have to add the -Recurse switch to the ‘Get-ChildItem’ cmdlet to receive all files recursively. Running the code push Powershell to look into all subfolders and regain all the list of files.
Method 4: Using Powershell to Delete Files Older Than x Days
Another method of disk space housekeeping is deleting files which are older than a specific number of days. We are able to tell from our experience that this method is useful for removing old log files. In this method, there are files in c:\temp which are older than 14 days. Using the script, the Name, CreationTIme, and AgeInDays of each file in c:\temp is shown.