How To Delete Files with Remove-Item and WMI in PowerShell

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

  1. Remove-Item Cmdlet on PowerShell
    1. Delete one file
    2. Delete All Files in the Folder
    3. Recursively Delete All Files
    4. Deleting Categorized Older Files 
    5. Matching and Deleting File Patterns 
  2. Workaround for Long Path Problem
  3. Removing Files with WMI on PowerShell
    1. Delete one file
    2. Delete all files in a directory
    3. 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

Remove-Item -Path "(filename.extension)"
Delete one file
  • 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
Recursively Delete All Files

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
Deleting Categorized Older Files 

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
Matching and Deleting File Patterns 

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 to C:\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"
Workaround for Long Path Problem
  • 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"
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 one file

Delete all files in a directory

PowerShell users can use the Get-CimInstance 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
Delete all files in a directory

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
Deleting files by an Extension

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. 

Meet the Author

Abdul Rahim has been working in Information Technology for over two decades. Learn how Abdul got his start as a Tech Blogger , and why he decided to start this Software blog. If you want to send Abdul a quick message, then visit his contact page here.