Ok, so you are running out of space on that drive and need to clean up. Of course you could jump into the GUI and manually select the files and folders but that is just so labour intensive. Fire up PowerShell and I’ll walk you through the Remove-Item cmdlet. Check out the TechNet article here: http://technet.microsoft.com/en-us/library/ee176938.aspx

The Remove-Item cmdlet does exactly what it says on the tin. It deletes one or more items and because it is supported by many providers, it can delete many different types of items, including files, directories, registry keys, variables, aliases, and functions.

Use “Get-Help Remove-Item –Full” for a full description of the cmdlet and the associated switches. You’ll notice that there are a number of Aliases associated with the cmdlet that we can utilise to save our fingers. If you just want some examples to show you what to do use “Get-Help Remove-Item –Examples”.


ri, rm, rmdir, del, erase, rd

So, instead of typing “Remove-Item deleteMe.txt” to delete the text file “deleteMe.txt” in the current working directory, we can just use one of the aliases and shorten the command to “ri deleteMe.txt”. Cool, huh. Use which ever alias makes sense to you. I tend to use “del” personally…

Ok, let’s have a look at some examples.

EXAMPLE 1: Remove all files from C:DeleteMeFolder. It is important to note that the use of the period (.) will not delete directories or files with no extension.

Remove-Item C:DeleteMeFolder*.*


EXAMPLE 2: Remove all word documents from the current directory BUT not any documents with “keepMe” in the filename that does not include “1”. The current directory is specified with the use of the wildcard and the command utilises the Include and Exclude parameters to specify the files to delete.

Remove-Item * -Include *.doc -Exclude *keepMe*


EXAMPLE 3: Remove a hidden and/or read only file from the path C:DeleteMeFolder, note the use of the Force parameter that forces permission.

Remove-Item -Path C:DeleteMeFolderHidden-ReadOnly-File.txt –Force


EXAMPLE 4: This command deletes all of the CSV files in the current directory and all subdirectories recursively. Because the Recurse parameter in the Remove-Item cmdlet has a known issue (it might not delete all child directories or files, especially if the ‘Include’ parameter is added to the command), the command in this example uses the Get-ChildItem cmdlet to get the desired files, and then uses the pipeline operator to pass them to the Remove-Item cmdlet. In the Get-ChildItem command, the Path parameter has a value of *, which represents the contents of the current directory. It uses the Include parameter to specify the CSV file type, and it uses the Recurse parameter to make the retrieval recursive. If you try to specify the file type in the path, such as “-path *.csv”, the cmdlet interprets the subject of the search to be a file that has no child items, and Recurse fails.

Get-ChildItem * -Include *.csv -Recurse | Remove-Item


EXAMPLE 5: We can even remove an old registry item! This command deletes the OldApp registry key and all of its subkeys and values. It uses the Remove-Item cmdlet to remove the key. The path is specified, but the optional parameter name (Path) is omitted. The Recurse parameter deletes all of the contents of the OldApp key recursively. If the key contains subkeys and you omit the Recurse parameter, you are prompted to confirm that you want to delete the contents of the key.

Remove-Item HKLM:SoftwareFakeCompanyOldApp -Recurse


EXAMPLE 6: And finally, sometimes permission errors on files or subfolders can cause the command to fail. Normally, you would sigh and painfully go through each folder manually in the GUI after taking ownership. However, by defining an ErrorAction we can simply keep going. Here we completely delete a folder along with files and sub-folders in it and force it to continue.

Remove-Item "C:DeleteMeFolder" -Force -Recurse -ErrorAction SilentlyContinue


I hope you find this useful and remember you are deleting stuff here. Be careful. In fact, you can perform a test run of your commands by using the -WhatIf variable. Try it out by simply adding -WhatIf to the end of any of the above examples and PowerShell will output the results of a dry run. In other words it will analyse and produce expected results without performing the action. Very, very handy variable.