The aliases of PowerShell Remove-Item are “ri”, “rm”, “rmdir”, “del”, “erase”, and “rd” which can be used in place of Remove-Item. This post provides insight into the PowerShell Remove-Item cmdlet.
How to use PowerShell Remove-Item cmdlet?
Remove-Item cmdlet permanently removes the item from a specific location on your system. The working of PowerShell Remove-Item can be understood with the help of the following syntax.
Syntax
Usually, the Remove-Item will remove the item placed at a specific path. The parameters offered by the Remove-Item cmdlet determine its advanced functionalities. The parameters supported by Remove-Item are as follows:
- Filter: To Remove-Item at command pipeline position.
- Include: To include the path of an element (to be deleted) or pattern like ” *.jpg“, “ *.doc” etc.
- Exclude: To exclude the path of an element or pattern such as ” *.jpg“, “ *.doc” etc.
- Recurse: To delete the folder along with subfolders.
- Force: It removes hidden or read-only files, aliases, and variables forcefully.
- Confirm: To confirm before executing the command
For a better demonstration of Remove-Item, we have exercised a set of examples.
Example 1: Remove an item from the present directory
This example demonstrates the functionality of the Remove-Item cmdlet from the current directory. For a better understanding, let’s get the content of our present directory (which is C:\Users\powershell\Documents\test\):
Keeping in view the content of the directory, we have executed the Remove-Item cmdlet to remove the “test.pub” file. After the Remove-Item command, we have used the ls command to list the files of the directory to ensure the deletion.
ls
The above output shows that the file “test.pub” has been removed from the current directory.
Example 2: Remove an item using its absolute path?
PowerShell Remove-Item also supports deleting the items with an absolute path. The following example code will explain the functionality of the command.
With the “ls” command we have listed all the items present in the test folder.
Suppose all the files with the “.rtf” extension is to be removed from the destination folder (i.e. C:\Users\powershell\Documents\test\). By using the “ls” command the deleted files can be verified.
ls C:\Users\powershell\Documents\test\
As you can observe from the above screenshot, the files with the “.rtf” extension have been removed from the directory.
Example 3: Remove All Items of the Directory
The wildcard is used to select all the files ending or starting with a specific character. You can remove all of the objects in the directory using the command listed below. Firstly, we are getting the content of the directory using the Get-ChildItem:
Get-ChildItem C:\Users\powershell\Documents\test\
The above snippet shows that all the items including .txt, .bmp, and .jpg of the test folder are removed.
Example 4: Remove Specific Items
The Include or Exclude parameters can be used to remove only specific files. This example demonstrates the usage of the Remove-Item cmdlet with Include and Exclude parameters.
Remove-Item with Include parameter
First of all list all the items in the test folders by using the Get-ChildItem command
As you can see the test folder has four different items including folder, JPG, DOC, and TXT files.
The following PowerShell command is used to delete the included files only. For instance, the files ending with “.jpg” and “.txt” will be removed only.
Get-ChildItem C:\Users\powershell\Documents\test\
According to the above-mentioned output, it can be noticed that the only specified file has been removed from the folder.
Remove-Item with Exclude parameter
While using the Remove-Item cmdlet, you can also exclude some of the items from the folder. Let’s get the content of the directory where we will apply the Remove-Item with Exclude parameter:
It can be noticed that there is only one “.txt” file and the rest of the folder’s other file formats.
Note: In PowerShell, the “ls” command and “Get-ChildItem” are used for listing purposes.
The following Remove-Item command will remove all the files excluding the “.txt” files in the directory.
Get-ChildItem C:\Users\powershell\Documents\test\
This time we will exclude the “.txt” files and the rest will be removed.
You can see the result attached above, the rest of the files have been removed from the folder except “.txt”.
Example 5: Remove the Hidden or Read-Only files
The Remove-Item cmdlet also supports removing the hidden or read-only files from the specified directory. First, we are listing down the items present in the directory (where the Remove-Item cmdlet will be applied): .
With “Get-ChildItem” we have examined all the items of the folder including hidden files. For the purpose to show the hidden files as well, we have used the “-Force” parameter after the Path.
Get-ChildItem C:\Users\powershell\Documents\test\ -Force
In this example the -Force parameter is used to remove all the hidden files whose extension is “.bmp”
As you can see there are two hidden files in the test directory. But when the command is run, the “.bmp” file has now been deleted.
Example 6: Remove the Folders and Subfolders Recursively
The PowerShell accepts the -Recurse parameter to remove all the items including subfolders.
The below code can be run for better understanding.
Get-ChildItem C:\Users\powershell\Documents\test\newFolder
In this code, the -Recurse parameter is used to remove all the subfolders within the folder.
It can be seen in the above screenshot, that there are two folders within the newFolder that have been deleted from the directory.
Great job! You have accomplished your goal of practicing on the PowerShell Remove-Item cmdlet.
Conclusion
The PowerShell Remove-Item is a command used for deleting items such as files, folders, keys, variables, and functions. We have discussed some of the common acceptable parameters of the Remove-Item with examples. In this article, you have learned how to use the Remove-Item cmdlet for deleting items using PowerShell.