Do you want to delete files and folders using PowerShell? We have you covered!
This article will describe two ways to delete files and folders. Choose the method that is most convenient for you.
1. Use the Remove-Item cmdlet
The Remove-Item cmdlet is a simple way to delete both files and folders. In fact, you can use it to delete any item, such as registry keys, aliases, and more. You can even pipe values to this cmdlet, as a part of your code, provided the LiteralPath parameter is not used. Also, note that this cmdlet returns no output.
Syntax
The syntax of Remove-Item is:
Remove-Item
[-Path] <String[]>
[-Filter <String>]
[-Include <String[]>]
[-Exclude <String[]>]
[-Recurse]
[-Force]
[-Credential <PSCredential>]
[-WhatIf]
[-Confirm]
[-Stream <String[]>]
[<CommonParameters>]
Let’s briefly look at each of the parameters, so you know what to use and when.
- Confirm: Asks you to confirm the deletion before proceeding to remove that item. This parameter is more a check to avoid accidental deletions.
- Exclude: As the name suggests, it excludes certain files or folders from the deletion process. You can provide a string array and wildcard characters as inputs.
- Filter: This removes only those files and folders that match the filters you specify. It supports wildcards and regular expressions.
- Force: This parameter forces PowerShell to remove items such as hidden or read-only files that can’t be accessed otherwise.
- Include: This parameter will include files and folders that meet your specifications. The input can be a string array or wildcard expression.
- LiteralPath: This parameter will treat a value as it is, and will not interpret wildcards.
- Path: This is used to specify the path of the file or folder.
- Recurse: This parameter deletes the files in the specified locations, as well as in all child items and references.
- Stream: This is a dynamic parameter available only on Windows and is used to delete from alternative data streams. Note that this parameter is not recommended due to possible security blocks.
- WhatIf: This parameter simply shows what will happen when the cmdlet is run, without actually executing it. This can come in handy when you want to examine possible scenarios.
Note that this cmdlet will automatically prompt for confirmation when you try to delete a folder with items in it. You can’t suppress this confirmation with any parameter.
Examples
Now that you know the parameters and syntax of Remove-Item, let’s look at some examples:
Remove All Text Files from a Folder
Remove-Item –path c:\myfolder\ remove-item * -include *.txt –recurse
This PowerShell command removes all the text files from a folder called “myfolder”
Pipe Inputs to Remove-Item
As mentioned, you can pipe inputs to this cmdlet.
Get-ChildItem C:\testdata -Recurse -Filter “mydata” | Remove-Item -Force -Recurse
This command removes a folder called “mydata” and its contents forcefully from “C:\testdata”
Get-ChildItem | Where-Object Name -Like ‘*`[*’ | ForEach-Object { Remove-Item -LiteralPath $_.Name }
Get-ChildItem
The above command removes all the files that contain special characters like parentheses and brackets.
Delete the Hidden and Read-only Files
Note that you must use the “force” parameter to delete hidden and read-only files.
Remove-Item -Path C:\Test\hiddenfile.txt -Force
To delete multiple files in the same command, simply enter all the file names separated by commas or input a string array with file names and their respective paths.
Thus, these are some ways to use the Remove-Item cmdlet.
2. Use the Delete() Method
Every object in PowerShell has a Delete() method and you can use it to remove that object.
Here, the object can be a file, folder, array, variable, registry keys, etc. The generic command is:
Object.Delete()
To delete files and folders, use the Get-ChildItem command and use the Delete() method on the output.
For example:
(Get-ChildItem C:\testdata.txt).Delete()
Here, this command will delete a file called “testdata.txt”.
Deleting Folders
The above command may not work for folders, especially if they are not empty.
For example, (Get-ChildItem C:\myfolder).Delete() will throw an error.
To resolve this error, pipe the value as input, and run it through a loop, like this:
Get-ChildItem C:\myfolder -Include *.* -Recurse | ForEach { $_.Delete()}
In the above command, you are returning all the files and sub-folders in the myfolder directory. The “Include” parameter includes all the files and subfolders within the folder while the “Recurse” parameter goes inside the sub-folders recursively and outputs each file inside them. The output is then piped to a ForEach loop. And inside this loop, you delete the file or sub-folder until there are no more files or sub-folders in the directory.
While this command deletes all the files and sub-folders in a folder, it does not delete the folder itself. Rather, it empties the folder and prepares it for deletion.
To check if a folder is empty, use the following PowerShell cmdlet.
Get-ChildItem C:\myfolder
This should not return any value. Now, you’re all set to delete the empty folder.
The command to delete an empty folder is:
(Get-ChildItem C:\myfolder).Delete()
Thus, this is how you can delete files and folders using the delete() method that comes as a built-in method with all objects.
Check the Result
Regardless of which method you use for deletion, check if the concerned file or folder is successfully deleted.
One way is to navigate to that particular location through Windows Explorer to check. But if you’d like to check through PowerShell itself, there are many cmdlets for it. Let’s look at a few now.
Test-Path cmdlet
This cmdlet returns true if the said folder exists in the given path and false if it doesn’t.
For example:
$path = C:\myfolder
Test-Path $path
This will return true if the folder exists in the given path and false otherwise. While running this cmdlet, make sure to give the right path.
File.Exists
A simple way to check if a file exists is to use the “Exists” command. This returns a true value if the file exists and a false if it doesn’t.
$path = C:\myfolder/testdata.txt
$path.Exists
There are also other ways to check if the file or folder is deleted, but these are the easiest ones.
Final Words
To conclude, there are two ways to remove files and folders using PowerShell.
You can either use the Remove-Item cmdlet of PowerShell or the Delete() method that comes by default with every PowerShell object, including folders and files. Both require some knowledge of PowerShell scripts to use the available parameters and efficiently remove files and folders from your system.
Make sure to check the results with “Exists” or “Test-Path” before moving on to other tasks.
We hope this was an insightful read for you. Make sure to check our other PowerShell guides as well.
Thank you for this.
Why is remove-item added after the path in the following line?:
Remove-Item –path c:\myfolder\ remove-item * -include *.txt –recurse
Based on the Microsoft Docs this doesn’t seem necessary?
or only try this :
rm -Recurse -Force .\myfolder-not-empty\
the above file does not explain how to delete files