Last Update: Sep 04, 2024 | Published: Jun 09, 2014
In this Ask the Admin, I’ll show you how to use Windows PowerShell to delete one or multiple folders from the command line. If you would like to move files using PowerShell instead, check out How Can I Move a File or Folder from the Command Line Using Windows PowerShell? on Petri.
The information in this article applies to Windows PowerShell, and PowerShell 7 and later versions on Windows 7, Windows 8.1, Windows 10, Windows 11, and all supported versions of Windows Server.
Let’s start by using the Remote-Item command to delete a single file or folder.
Here is the syntax for Remove-Item:
Remove-Item
[-Path] <String[]>
[-Filter <String>]
[-Include <String[]>]
[-Exclude <String[]>]
[-Recurse]
[-Force]
[-Credential <PSCredential>]
[-WhatIf]
[-Confirm]
[-Stream <String[]>]
[<CommonParameters>]
Here is a brief description of what each of the parameters does:
Parameter | Description |
-Confirm | Prompts you to confirm the operation before running the command. |
-Exclude | Used to exclude a string array, item, or items from the operation. |
-Filter | Remove files and folders that match the specified filter. It works with wildcards and regular expressions. |
-Force | Include this parameter if you want to remove items like hidden or read-only files. |
-Include | Use to include a string array, items, or item that should be affected by the operation. |
-LiteralPath | Specify a path to one or multiple locations without interpreting characters as wildcards. |
-Path | Specify the location for items to be removed. |
-Recurse | Enumerate all child items in the specified locations. |
-Stream | Used to remove alternative data, like Zone.Identifier, from directories and files. Although it is not the recommended way to prevent security checks. Unblock-File is the preferred method. |
-WhatIf | Shows you want would happen if the cmdlet runs but without actually running it. -WhatIf is good for testing. |
If you try to delete a folder using Remove-Item that contains items, you will always be prompted to confirm the operation.
Make sure you are logged in to the server or PC with an account that has full access to the objects you want to delete.
The –recurse parameter will allow PowerShell to remove any child items without asking for permission. Additionally, the –force parameter can be added to delete hidden or read-only files.
For more information on how to manage file and folder permissions using PowerShell, check out How to Use PowerShell to Manage Folder Permissions on Petri.
In the PowerShell prompt, type Remove-Item –path c:testfolder remove-item * -include *.mp3 –recurse and press Enter. This command removes all MP3 files from the testfolder directory and any subdirectories.
It’s also possible to include a filter, like this:
Remove-Item –path c:* -Filter *test* -whatif
Any folder in the root of the C drive with the word test in its name will be deleted. The –whatif parameter tests the command and lets you see the results, so you can be sure you won’t delete anything important. To actually run the delete operation, just remove the –whatif parameter.
Remove-Item –path c:testfolder* -include *.txt
The above command will remove all .txt files in the testfolder directory. There is also an –exclude parameter, which comes in handy when used in combination with filters. Finally, to specify the current directory as shown in the PowerShell prompt, just replace the –path parameter with a wildcard symbol as follows:
Remove-Item * -Filter *test*
Related articles