Windows PowerShell Remove-Item

Windows PowerShell Remove-Item (Del)

Warning:  I have been unusually cautious on this page, because if a delete script goes wrong and forces a delete of the wrong files it could be disastrous.  Hundreds of people will succeed no trouble, but I do worry about that one person who gets the wrong end of Remove-Item and deletes the wrong files.

Note 1: There is no PowerShell Delete-Item cmdlet it’s Remove-Item

Windows PowerShell Remove-Item Progression

 ♣

Delete-Item Safety Measures

Before deleting files or erasing stuff, I advise you to see what happens with Get-Item before you unleash Remove-Item.

When testing a deletion script my idea is to create folder called C:\GuyStuff\ then create a few files in this folder.

PowerShell Script To Create Test Folder and Files

This is in preparation, my idea is that we delete test files and not existing ones.

# PowerShell Create Folder and Files
Clear-Host
New-Item C:\GuyStuff -type directory -ErrorAction SilentlyContinue
for ( $i = 1; $i -le 5; $i+=1 ) {
New-Item C:\GuyStuff\BadFile$i.txt -type file -ErrorAction SilentlyContinue
}
Write-Host "$i Files created"

Preparation – Run Get-Item

#PowerShell Script to List Files
Clear-Host
Get-Item C:\GuyStuff\*.*

Actually Delete Files – PowerShell Remove-Item Example

#PowerShell Remove-Item Example to Delete Files
Clear-Host
$BadFile = Get-Item c:\GuyStuff\*.*
$BadFile | Remove-Item -force
Get-Item C:\GuyStuff\

Note 2:  This script features the -force switch to zap protected files.

Note 3: The verb is remove, there is no such built-in cmdlet as Delete-Item.

Note 4:  For an advanced real-life Remove-Item script see here.

Guy Recommends:  A Free Trial of the Network Performance Monitor (NPM)Review of Orion NPM v11.5 v11.5

SolarWinds’ Network Performance Monitor will help you discover what’s happening on your network.  This utility will also guide you through troubleshooting; the dashboard will indicate whether the root cause is a broken link, faulty equipment or resource overload.

What I like best is the way NPM suggests solutions to network problems.  Its also has the ability to monitor the health of individual VMware virtual machines.  If you are interested in troubleshooting, and creating network maps, then I recommend that you try NPM now.

Download a free trial of Solarwinds’ Network Performance Monitor

Remove-Item -WhatIf Safety Parameter

One of my very few warnings is before you use Remove-Item for a real task append the -WhatIf switch.

#PowerShell Script to Mimic Deleting Files
Clear-Host
$BadFile = Get-Item c:\GuyStuff\*.*
$BadFile | Remove-Item -WhatIf -force
Get-Item C:\GuyStuff\*.*

Further Research on Remove-Item

PowerShell’s Remove-Item Parameters

# PowerShell Remove-Item Parameters
Clear-Host
Get-Help Remove-Item -full

Checking the help file will reveal useful parameters, for instance -force -exclude.  Dare I tell you that there is also -recurse, but in the case of Remove-Item, use that parameter with the utmost care.

Remove-Item Aliases Del and Erase

Check the aliases with this little script:

# PowerShell Remove-Item Alias
Clear-Host
Get-Alias -definition Remove-Item

Note 5: There is no actual PowerShell Delete-Item cmdlet, however, there is a ‘del’ alias.

With Microsoft, there are always at least three ways of doing everything, what seems like redundancy when you are an expert, seems like perspective when you are a beginner.  As you increase your range of PowerShell commands, keep an eye out for another PowerShell Alias, for example gci (Get-Childitem).

Engineer's Toolset v10Guy Recommends: SolarWinds Engineer’s Toolset v10

This Engineer’s Toolset v10 provides a comprehensive console of 50 utilities for troubleshooting computer problems.  Guy says it helps me monitor what’s occurring on the network, and each tool teaches me more about how the underlying system operates.

There are so many good gadgets; it’s like having free rein of a sweetshop.  Thankfully the utilities are displayed logically: monitoring, network discovery, diagnostic, and Cisco tools.  Try the SolarWinds Engineer’s Toolset now!

Download your fully functional trial copy of the Engineer’s Toolset v10

Real-life Tasks for Remove-Item

Microsoft's Windows operating systems create temporary files, let us use PowerShell's Remove-Item to clean up these unwanted files.

Researching Similar PowerShell Cmdlets

# PowerShell Item Cmdlet Research
Clear-Host
Get-Command -Noun Item

As we know there is a Get-Item and a New-Item cmdlet, but you may not realize there is a Copy-Item cmdlet.  PowerShell -Noun or -verb research always throws up at least one surprise.  In this case it’s the absence of a PowerShell Delete-Item cmdlet, use Remove-Item instead.

PowerShell Remove PSDrive ยป

Summary of PowerShell Remove-Item

The syntax for Remove-Item is straightforward.  I urge you to take precautions because if you get the path wrong, the effect could be devastating, especially if you are gung-ho with the -force and the -recurse parameter.  For this reason I suggest that you check with Get-Item before you actually use Remove-Item, even then consider the -whatIf switch if there is any doubt.

If you like this page then please share it with your friends

 


See more PowerShell examples for syntax constructions

PowerShell Tutorials   • Syntax   • Pipeline   • Quotes   • Remove-Item   • ItemProperty

Select-String  • -replace string   • Group-Object   • Sort-Object   • PowerShell Splatting

Windows PowerShell cmdlets   • Windows PowerShell  • New-Item  • PowerShell New Object

Please email me if you have a better example script. Also please report any factual mistakes, grammatical errors or broken links, I will be happy to correct the fault.