The Powershell remove-item cmdlet can be used to delete files, but it also can be used to remove other objects such as folders, registry keys and powershell variables. I’ve written previously about how to delete a file after checking it exists using PowerShell, so here I will be giving some examples of how remove-item can be used on it’s own.
Deleting a File using Remove-Item
The simplest remove-item example is for removing a single file. This can be done using the following syntax if you are in the directory where the file exists:
$ Remove-Item file.txt
Otherwise you can specify an absolute path to the file when using the command:
$ Remove-Item C:\scripts\file.txt
You can also delete multiple files at the same time. One way to do this would be to delete all files that have a particular file extension. For example:
$ Remove-Item C:\scripts\*.txt
This would delete all files in that location with a .txt
extension. Alternatively we could delete a list of files by separating each file name with a ,
as shown here:
$ Remove-Item .\1.txt, .\2.txt,.\3.txt
Let’s move on to looking at how to delete directories.
Delete Recursively to Remove Folders
We can use the recursive option to delete a folder and all the contents it contains. It goes without saying that you should exercise caution here! To delete a directory and it’s contents using remove-item we can do the following:
$ Remove-Item -Recurse C:\oldfiles
If the -Recurse option is omitted when attempting to delete a folder you will be informed if the folder is not empty and prompted to remove it’s contents:
$ remove-item .\files\
Confirm
The item at C:\Scripts\files\ has children and the Recurse parameter was not specified. If you continue, all children will be removed with the item. Are you sure you want to continue?
[Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "Y"):
Delete Hidden or Read-Only Files
You can delete hidden or read-only files using the Powershell remove-item cmdlet by adding the -Force
option to the command:
$ Remove-Item -Path C:\scripts\my-hidden-read-only-file.txt -Force
Without the -Force option, read only or hidden files cannot be deleted with the remove-item cmdlet.
Selectively Deleting Files or Folders
The -include and -exclude options can be used with the remove-item cmdlet to help you selectively delete files from within a folder. For example, if you wanted to delete all the files with a .txt extension apart from one called myfile.txt you could use:
$ Remove-Item * -Include *.txt -Exclude *myfile*
Delete a Registry Key using PowerShell
Remove-item isn’t just for files and directories, it can also be used to delete items in the Windows registry. Rather than specifying the file name you specify the registry key name. For example:
$ Remove-Item HKLM:\Software\Windows\MyRegKey
As with files and folders, you can also use the -recurse
option to delete any sub keys.
Remove-Item -Confirm!
The remove-item cmdlet allows you to use the -confirm
option, which is useful when testing to ensure that your command is going to do what you think it is going to do. Rather than go ahead and delete any files or items that are in scope it will prompt for confirmation:
$ Remove-Item * -Include *.txt -Exclude *2* -confirm
Confirm
Are you sure you want to perform this action?
Performing the operation "Remove File" on target "C:\Scripts\1.txt".
[Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "Y"):
Summary
This article has shown a number of ways in which the remove-item powershell cmdlet can be used to delete files, folders and other items such as Windows registry keys.