Guy recommends :
Free Solarwinds
VM Console

Solarwinds VM Console Free Download

Find out which of your VMs are a waste of space and which VMs need more resources.



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: 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 1:  This script features the -force switch to zap protected files.

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

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

Guy Recommends:  Solarwinds' Free Bulk Import ToolFree Download of Solarwinds  Bulk Import Tool

Import users from a spreadsheet.  Just provide a list of the users with their fields in the top row, and save as .csv file.  Then launch this FREE utility and match your fields with AD's attributes, click to import the users.  Optionally, you can provide the name of the OU where the new accounts will be born.

There are also two bonus tools in this free download, and all 3 have been approved by Microsoft:

  1. Bulk-import new users into Active Directory.
  2. Seek and zap unwanted user accounts.
  3. Find inactive computers.

Download your FREE bulk import tool.

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 4: 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).

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.

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  • New-Item  • Remove-Item  • ItemProperty

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

Windows PowerShell cmdlets   • Windows PowerShell

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.

Download my ebook:Getting Started with PowerShell
Getting Started with PowerShell - only $9.25

You get 36 topics organized into these 3 sections:
   1) Getting Started
   2) Real-life tasks
   3) Examples of Syntax.

In addition to the ebook, you get a PDF version of this  Introduction to PowerShell ebook  It runs to 120 pages of A4.

 *


Custom Search

Guy Recommends: WMI Monitor and It's Free!Solarwinds WMI Monitor

Windows Management Instrumentation (WMI) is one of the hidden treasures of Microsoft operating systems.

Fortunately, Solarwinds have created the Free WMI Monitor so that you can actually see and understand these gems of performance information.  Take the guess work out of which WMI counters to use for applications like Microsoft Active Directory, SQL or Exchange Server.

Download your free copy of WMI Monitor

 

Home Copyright © 1999-2012 Computer Performance LTD All rights reserved

Please report a broken link, or an error.