Using PowerShell To Delete Files With Remove-Item and WMI
Using PowerShell To Delete Files With Remove-Item and WMI
Using PowerShell To Delete Files With Remove-Item and WMI
June Castillote
Read more posts by this author.
https://lazyexchangeadmin.cyou/
junecastillote
Maintaining free disk space is crucial when managing servers and systems. As admins,
you wouldn’t want to get caught unaware of a ‘disk full’ situation. To ensure you’re in
the clear, you should learn how to use PowerShell to delete files!
In this article, you will learn just about every way you to remove files from your systems
Adam the AutomatorLooks like you're offline!
with PowerShell.
Table of Contents
Prerequisites
Next Steps
Further Reading
Prerequisites
Adam the AutomatorLooks like you're offline!
This article presents examples using PowerShell, and if you plan to follow along, you
will need the following.
Did you know that the Remove-Item cmdlet has an alias by the name of del? When
working in PowerShell, using Remove-Item or del will run the same command.
Running the code above in PowerShell would not show anything on the screen unless an
error was encountered.
Running the above code forces PowerShell to look into all sub-folders and retrieve all
the list of files. The output below shows that the code was able to delete files in the top
folder; however, it failed to retrieve the files in the sub-folders.
Adam the AutomatorLooks like you're offline!
In this case, the error was because the path that the Get-ChildItem is trying to read
exceeds the maximum path length of 260 characters.
The screenshot below shows that the path or directory and its sub-directories exist and
one text file named InTooDeep.txt is located in the bottom-most sub-directory. The
combination of all the characters that make up the nested directory names creates a
long path problem.
Adam the AutomatorLooks like you're offline!
In Windows PowerShell 5.1, there is a workaround to the long path name problem. The
workaround is to use the Unicode version of the path. Instead of specifying the path like
this – C:\temp, use the Unicode version like this – ‘\\?\C:\temp’ for folders located
locally, or ‘\\?\UNC\<computername>\<share>\Temp’ if the folder is located in a UNC
path.
Using the modified code above that caters to the long path name, the output below
shows that PowerShell read the deeply nested path name successfully and deleted the
file.
Note that the long path name problem does not affect PowerShell 7.0. With
PowerShell 7.0, there is no need to use the Unicode version of the path because it
already has built-in support for long path names.
If the folders need to be deleted, too, just remove the -File parameter from the Get-
ChildItem cmdlet, and PowerShell should delete all items, including files and folders.
In this example, there are files in c:\temp that are older than 14 days. Using the script
below, the Name , CreationTIme , and AgeInDays of each file in c:\temp is shown.
As you can see in the screenshot below, there are files that are 15 days old, 7 days old,
and 0 days old.
Adam the AutomatorLooks like you're offline!
Now that you know the files to remove, you can create a script to only delete files that
are older than a specific number of days – in this case, older than 14 days.
In this example script below, files in C:\temp whose CreationTime value is older than the
set threshold will be deleted.
The first line defines the path for Get-ChildItem to search. The path is saved in the $path
variable. Then, the second line is where the threshold is specified. The value of the
$threshold the age in days that the file to be deleted must be.
Get the collection of files located in the folder specified in the $path variable. In
this example, the path is C:\temp.
Filter the output to include only files whose CreationTime value is older than
Adam the Automator Looks like you're offline!
the number of days saved in the $threshold variable. In this example, the
threshold is 14 days.
Pipe the filtered list of files to the Remove-Item value to perform deletion of
those files.
$path = 'C:\Temp'
$threshold = 14
When you run the above code you will see output like shown below.
Notice that from the screenshot above, based on the verbose message, the script only
deleted five files older than 14 days. To confirm that files newer than 14 days still exist,
run the code below to list them again.
This next example below shows how to delete the files that match *.LOG filename. One
way of doing it by using the Remove-Item cmdlet directly with the use of the -Include
Another way is, perhaps, the cautious approach is to use Get-ChildItem first to collect the
list of files to be deleted. Only when you’re satisfied with the list of files for deletion, you
can finally pipe the collection to the Remove-Item cmdlet.
For example, the code below gets that *.LOG files in c:\temp.
As a result of the code above, Get-ChildItem returns a list of files matching the *.LOG
filename.
But, what if you want to exclude a file that contains the number 5 in its name? You can
do so by adding the -Exclude parameter like this next code.
Since you excluded the file with the number 5, the result is now different. Specifically,
the file File_5.log is no longer on the list, as shown below.
Adam the AutomatorLooks like you're offline!
When you’re already satisfied with the collection of files that your code creates, then you
can pipe the files collection to the Remove-Item cmdlet to finally delete those files.
After running your final code, you will have achieved your goal of deleting only the files
you selected.
Adam the AutomatorLooks like you're offline!
PowerShell comes with support for WMI. And support for WMI means that WMI
queries and methods can be called from within PowerShell. Yes, WMI is not just for
Visual Basic scripts that admins used in the earlier days of Windows.
Microsoft released WMI-specific CIM cmdlets in PowerShell 3.0. The CIM cmdlets that
will be used to delete files are the Get-CimInstance and Invoke-CimMethod .
CimInstance cmdlet with the Cim_DataFile class is used to retrieve the information about
the file to delete which is C:\Temp\random.txt.
$file2delete Adam the Automator
= Get-CimInstance Looks like you're
-ClassName offline!
Cim_DataFile -Filter "Name = 'C:\Temp\rando
$file2delete
In the above code, the -Filter parameter accepts a WQL format query. Using WQL
requires that you escape some characters including the backslash. And, since the WQL
escape character is also the backslash, resulting in the double-backslash characters –
\\ .
Running the code above produces the result shown in the demonstration below. The
information about C:\Temp\random.txt is saved to the $file2delete variable
Now that the information for the file C:\Temp\random.txt is retrieved, the resulting
object in the $file2delete variable can be piped to the Invoke-CimMethod cmdlet. The
Invoke-CimMethod cmdlet has a parameter called -Name , which represents the name of the
method of the Cim_DataFile class.
As you see in the screenshot below, the ReturnValue shows 0, which means that the
command was successful.
Adam the AutomatorLooks like you're offline!
To know about the possible ReturnValue codes, please refer to this link – Delete
method of the CIM_DataFile class
Additionally, the screenshot below shows that after running the Invoke-CimMethod
Delete() method, CIM only removed the C:\Temp\random.txt file. It did not remove
the other files.
In this next code, the Get-CimInstance cmdlet retrieves all files located in C:\temp. As
you can see below, the query filters the Drive and Path properties of the Cim_DataFile
class.
Running the code above saves the retrieved information about the files in C:\temp in
the $file2delete variable. Viewing the value(s) of the $file2delete variable shows the
output below.
Remember, the ReturnValue code of 0 means successful, and for each file that the delete
method was called on will have its own ReturnValue code.
You’ll notice in the code below, this time the query has an added condition ( Name LIKE
'%.log ). This added condition means that only files that match the .LOG extension is
returned. The percent ( % ) sign is a WQL LIKE operator that means “A string of zero or
more characters”. In programming terms, the % is the equivalent of a wildcard, which is
the asterisk ( * ) character.
-Filter "Drive = 'c:' AND Path = '\\temp\\' AND Name LIKE '%.log'"
The demonstration below shows that before the code above is executed, there are nine
*.LOG files and only one *.TXT file. Once the code is done running, the *.LOG files are
deleted and the *.TXT file is the only file left in the folder.
Adam the AutomatorLooks like you're offline!
## List all files in C:\Windows\Web\ recursively using Get-CimInstance and WMI Query
When you run the code above in PowerShell, you’ll see the output similar below.
Adam the AutomatorLooks like you're offline!
As you can see from the output shown above, listing the files in C:\windows\web
almost took ten times longer using Get-CimInstance than the time it took for Get-
ChildItem to complete!
Also, did you notice how the Get-ChildItem line is much shorter than Get-CimInstance ?
Not only you get faster execution using Get-ChildItem , but you also benefit from a
cleaner and shorter code.
Next Steps
In this article, you’ve seen the two different ways you can use PowerShell to delete files
with built-in cmdlets and WMI/CIM.
Know that you Adam
shouldthe
always use the
Automator
Get-ChildItem and Remove-Item cmdlet to remove
Looks like you're offline!
files. These built-in cmdlets are more flexible, easier, and faster to use than when using
WMI.
Try to come up with a script that can perform disk space housekeeping for you. Sure
some scripts already exist for that purpose; by all means, use them as a reference, but if
you’re willing to practice and learn, you should try to create your own.
Further Reading
CIM_DataFile class
SUBSCRIBE
We’ve put together a list of the resources we, at ATA, can wholeheartedly
recommend!
Adam the AutomatorLooks like you're offline!
What do you think of this post?
2 0 1 2 1 0
Josh,