0% found this document useful (0 votes)
46 views

Delete Files in PowerShell

This document discusses various methods for deleting files in PowerShell. It describes using the Remove-Item cmdlet to delete single files and multiple files at once by specifying file paths and wildcards. It also covers deleting files with a specific extension, excluding certain files, and checking the folder contents while deleting. The document concludes by explaining how to delete files using Windows Management Instrumentation commands in PowerShell.

Uploaded by

haisu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views

Delete Files in PowerShell

This document discusses various methods for deleting files in PowerShell. It describes using the Remove-Item cmdlet to delete single files and multiple files at once by specifying file paths and wildcards. It also covers deleting files with a specific extension, excluding certain files, and checking the folder contents while deleting. The document concludes by explaining how to delete files using Windows Management Instrumentation commands in PowerShell.

Uploaded by

haisu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

HOME LEARNING SUBSCRIBE!

WRITE FOR US PRIVACY TERMS 

Powershell

Delete Files in PowerShell


3 months ago • by Talha Saif Malik

PowerShell is Microsoft’s automation and scripting platform. It is a .NET Framework-based


scripting language as well as an interactive command environment. PowerShell consists of a
set of commands that perform specific functions. Just like any programming language,
PowerShell can accomplish a lot of tasks.

When it comes to managing systems and servers, having enough free storage space is
critical. As an administrator, you don’t want yourself to face the “disc full” situation. You
should understand how to delete files in PowerShell to make sure you are clear!

Delete Files in PowerShell using Remove-Item cmdlet


In PowerShell, the Remove-Item cmdlet deletes one or more items from the list. It utilizes the
path of a file for the deletion process. Using the “Remove-Item” command, you can delete
files, folders, variables, aliases, registry keys, etc.
To demonstrate the process file deletion in PowerShell, we have created some test files
named: testfile1.txt, testfile2.txt, and testfile3.txt.

Deleting a single file in PowerShell


The “-Path” option is used in the “Remove-Item” command to provide the file’s location that
we want to delete. In the below-given example, we are going to delete the “testfile1.txt”
using the “Remove-Item” cmdlet:

> Remove-Item -Path E:\testfile1.txt

Deleting multiple files at once in PowerShell


Our “testfolder1″ contains some files, which we want to delete at once. To do so, in our
“Remove-Item” command, we will add “.” at the end of the folder path.

> Remove-Item E:\testfolder1\*.*


Execution of the above-given command will delete all the files present in “testfolder1” at
once.
Check folder content while deleting files in PowerShell
In PowerShell, “Get-ChildItem” performs the same function as “dir” in the Windows
command prompt. This cmdlet can retrieve the content of a folder by listing out objects or
items from the provided location. PowerShell also gives you the facility to view the content of
the folder while deleting them. This combination of commands is helpful if you want to ensure
that the file is deleted.

“-Path” option is utilized to specify the location of the particular file we want to delete.
“-File” option specifies that files are the only type of item to be included.
“-Verbose” option will show that the folder intended to delete has been deleted or not.

> Get-ChildItem -Path E:\testfolder1 -File | Remove-Item -Verbose


This command comprises the “Get-ChildItem” command to retrieve the child item of a folder
and pass it to the “Remove-Item” cmdlet using a pipe operator [“|”]. That’s how the files
present in the “testfolder1” are going to be deleted.
You can also add the “-Recurse” option in the same command. This option will search for the
files and folders in the subdirectories of the specified path.

> Get-ChildItem -Path E:\testfolder1 -File -Recurse | Remove-Item -Verbose

Delete Files in PowerShell with a specific extension


The “-Include” is a string parameter utilized by the “Remove-Item” cmdlet to delete specific
files based on specific extensions. We will execute the below-given command to delete all
files with the “.txt” extension present in “tesfolder1”. The wildcard “*” is used with “.txt” to
specify all the files having the “.txt” extension, and with the “-Path” parameter, this wildcard
specifies the content of the folder.

> Remove-Item -Path E:\testfolder1\* -Include *.txt


The “-Exclude” is a string parameter used to exclude files with some specific extension or
wildcards. It is specified after adding the path of the directory. Here, we will exclude the “.txt”
files having “1” in their file names. Other than that, this execution of this command will delete
all files present in the “testfolder1”.

> Get-ChildItem -Path E:\testfolder1\* -Include *.txt -Exclude *1* | Remove-Item -Verbose

Delete Files in PowerShell using WMI


Window Management Instrumentation (WMI) is supported by PowerShell, which means
that WMI methods and queries can be called directly from PowerShell. WMI isn’t just for
admins who utilized Visual Basic scripts in the early days of Windows. In PowerShell,
Microsoft included WMI-specific CIM cmdlets. The Get-CimInstance and Invoke-CimMethod
are used to delete.

$file2delete = Get-CimInstance -ClassName Cim_DataFile -Filter "Name =


'E:\\testfolder1\\testfile2.txt'"

$file2delete
The “Get-CimInstance” utilizes the “Cim_DataFile” to extract the information related to

“E:\\testfolder1\\testfile2.txt”.

As the information for the file “ E:\\testfolder1\\testfile2.txt” has been received, the
“$file2delete” variable can be used to pass the resulting object to the Invoke-CimMethod
cmdlet. The “-Name” option of the Invoke-method cmdlet specifies the name of the method
of the Cim_DataFile class.

The output declares that the selected file is successfully deleted!

Conclusion
If you are tired of those rigid files that cannot be deleted easily, you can now use PowerShell
to get rid of them. PowerShell provides several commands and techniques to delete a file.
This post shows you some methods for deleting a file using the “Remove-Item” cmdlet and
“WMI.” To delete files, you should always use the “Get-ChildItem” combined with “Remove-
Item” cmdlets. When compared to WMI, these built-in cmdlets are easier, flexible, and faster
to utilize.
ABOUT THE AUTHOR

Talha Saif Malik


Talha is a contributor at Linux Hint with a vision to bring value and do useful
things for the world. He loves to read, write and speak about Linux, Data,
Computers and Technology.

View all posts

RELATED LINUX HINT POSTS

PowerShell Start-Process

PowerShell Split String

PowerShell Select-Object

PowerShell Operators

PowerShell Not Equal

PowerShell Get-Content

PowerShell Comments

Linux Hint LLC, [email protected]

1210 Kelly Park Cir, Morgan Hill, CA 95037

AN ELITE CAFEMEDIA PUBLISHER

You might also like