Deleting files using PowerShell can be more efficient than doing it manually, especially when dealing with a large number of files. In this article, we will explore eight different solutions to delete a file with PowerShell, including various cmdlets and techniques that can be used in different scenarios.
Requirements to Delete A File With PowerShell
Before attempting to delete a file on your Windows PC through PowerShell, you will have to make sure that your system has the following factors available in it –
- A computer with an OS version of Windows 10 or Windows 11
- Windows PowerShell version 5.2 or PowerShell 7.0
- Script line editor; Options we recommend are Visual Studio Code and Atom.
Windows Management Instrumentation vs Remove-Item
Windows Management Instrumentation (WMI) and Remove-Item are two different concepts in PowerShell. WMI is a technology for system management and Remove-Item is a cmdlet for file management.
WMI provides a standardized way of querying and modifying system settings and data. You can use WMI to gather information about various aspects of a system like hardware and software.
Although WMI can be used for file manipulation, it’s generally not recommended to do so, as there are simpler and more reliable ways to manage files and folders in PowerShell.
Remove-Item, on the other hand, is a cmdlet in PowerShell that allows you to delete files and folders. It is typically used to remove unwanted or obsolete files from a system.
Methods to delete a file through PowerShell
- Remove-Item Cmdlet on PowerShell
- Delete one file
- Delete All Files in the Folder
- Recursively Delete All Files
- Deleting Categorized Older Files
- Matching and Deleting File Patterns
- Workaround for Long Path Problem
- Removing Files with WMI on PowerShell
- Delete one file
- Delete all files in a directory
- Deleting files by an Extension
Deleting Files in PowerShell
There are two ways you can use to delete files on your computer through Windows PowerShell. You can use either the WMI or the Remove-Item
cmdlet. If you are trying to choose between either of these, take a look into the comparison paragraph above.
Remove-Item Cmdlet on PowerShell
It’s important to exercise caution when using the Remove-Item
cmdlet, as it permanently deletes files and folders without the ability to recover them. Make sure you specify the correct path before running the command.
Delete one file
- Open Windows PowerShell by typing “powershell” in the Start menu and selecting the “Windows PowerShell” app.
- Navigate to the directory that contains the file you want to delete using the cd command. For example, if the file is located in the Documents folder, type cd
C:\Users\username\Documents
and press Enter. - Once you are in the directory containing the file you want to delete, type the following command and press Enter:
Remove-Item -Path "(filename.extension)"
- Confirm that you want to delete the file by typing “Y” and pressing Enter.
Delete All Files in Folder
- Open Windows PowerShell by typing “powershell” in the Start menu and selecting the “Windows PowerShell” app.
- Navigate to the directory that contains the files you want to delete using the cd command. For example, if the files are located in the Documents folder, type
cd C:\Users\username\Documents
and press Enter. - Once you are in the directory containing the files you want to delete, type the following command and press Enter –
Remove-Item *
- Confirm that you want to delete the files by typing “Y” and pressing Enter.
In addition to this, you can also use the Get-ChildItem
cmdlet if you are trying to just get rid of just files. If this is used, all subfolders will be left alone. An example script for this can be seen below –
Get-ChildItem -Path C:\temp -File | Remove-Item -Verbose
Recursively Delete All Files
To recursively delete all files in a folder and its subfolders using the Remove-Item
cmdlet in PowerShell, you can use the -Recurse parameter along with the -Force
parameter to avoid any prompts for confirmation.
Your command to delete all files in a directory recursively should look something like this –
Get-ChildItem -Path C:\temp -File -Recurse | Remove-Item -Verbose
Deleting Categorized Older Files
To delete categorized older files using the Remove-Item
cmdlet in PowerShell, we can use the CreationTime
property and the -Recurse
and -Force
parameters.
Here’s an example:
Get-ChildItem -Path "C:\MyFolder" -Recurse |
Where-Object { $_.CreationTime -lt (Get-Date).AddDays(-30) -and $_.PSIsContainer -eq $false } |
Remove-Item -Force
The above-mentioned example command deletes all files that match the filter, regardless of their category. If you want to delete files of a specific category, you need to modify the filter accordingly.
Matching and Deleting File Patterns
To match and delete file patterns using PowerShell, you can use the Get-ChildItem cmdlet to retrieve a list of files based on a specified pattern and then pipe the output to the Remove-Item cmdlet to delete the matched files.
Here’s an example of how to match and delete all files in a folder that has the extension “.txt”:
Get-ChildItem -Path C:\Files\*.txt | Remove-Item
In this example, the Get-ChildItem
cmdlet retrieves all files in the “C:\Files” folder that have the “.txt” extension, and the output is piped to the Remove-Item
cmdlet to delete the matched files.
Note: It’s important to be cautious when using the Remove-Item cmdlet to delete files, especially when using wildcard patterns.
Workaround for Long Path Problem
When using the Remove-Item cmdlet in PowerShell to delete files with a long path, you may encounter an error stating that the path is too long. This is a common issue, especially when working with deeply nested files or directories.
One workaround for this problem is to use the Windows API function PathCreateFromUrl to create a temporary symbolic link with a shorter path. Here are the steps to follow:
- Create a new directory on your system to act as the base path for the symbolic links. For example, you could create a directory called C:\symlink.
- Use the
New-Item
cmdlet to create a new symbolic link in the C:\symlink directory that points to the directory or file you want to delete. For example, to create a symbolic link toC:\very\long\path\to\file
.txt, you could use the following command:
New-Item -ItemType SymbolicLink -Path "C:\symlink\file.txt" -Target "C:\users\AR\Minecraft\files\test.txt"
- Use the
Remove-Item
cmdlet to delete the symbolic link you just created. This will delete the original file or directory as well.
Remove-Item -Path "C:\symlink\file.txt"
Removing Files with WMI on PowerShell
Delete one file
Use the Get-CimInstance
cmdlet with the Cim_DataFile
parameter to get the file you want to delete. You can specify the path and file name of the file you want to delete. For example:
$file = Get-CimInstance -ClassName Cim_DataFile -Filter "Name='C:\Users\John\Documents\example.txt'"
Delete all files in a directory
PowerShell users can use the Get-CimInstanc
e cmdlet with the Cim_DataFile
parameter to get all the files in the folder. Specify the folder path using the Path parameter. For example:
$files = Get-CimInstance -ClassName Cim_DataFile -Filter "Path='C:\\Path\\to\\folder\\'" -Recurse
This command retrieves all the files in the specified folder and its subfolders and stores them in the $files
variable.
Note: Be cautious when using WMI to delete files as it does not prompt for confirmation before deletion. It is recommended to test the script on a test folder before running it on production data.
Deleting files by an Extension
To delete a certain extension file through WMI in PowerShell, you can use the Get-CimInstance
cmdlet with the CIM_DataFile
class and filter for files with the desired extension using the Filter parameter. Then, you can use the Remove-CimInstance
cmdlet to delete the files.
Here is an example code snippet that deletes all files with the extension “.txt” in the directory “C:\ExampleFolder”:
Get-CimInstance -ClassName CIM_DataFile -Filter "Drive='C:' AND Path='\\ExampleFolder\\' AND Extension='txt'" | Remove-CimInstance
Note: You may need to run PowerShell with administrative privileges to delete certain files.
By now, you should have got rid of all the files you no longer need on our Windows computer. If you have further queries, you can contact the Windows developers on your computer.