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

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/Delete-Files-in-PowerShell-1.png" data-lazy- height="338" src="data:image/svg xml,” width=”931″>

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.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/Delete-Files-in-PowerShell-2.png" data-lazy- height="309" src="data:image/svg xml,” width=”927″>

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.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/Delete-Files-in-PowerShell-3.png" data-lazy- height="332" src="data:image/svg xml,” width=”925″>

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

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/Delete-Files-in-PowerShell-4.png" data-lazy- height="332" src="data:image/svg xml,” width=”927″>

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

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/Delete-Files-in-PowerShell-5.png" data-lazy- height="282" src="data:image/svg xml,” width=”928″>

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

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/Delete-Files-in-PowerShell-6.png" data-lazy- height="325" src="data:image/svg xml,” width=”1040″>

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:testfolder1testfile2.txt'”

$file2delete

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

“E:\testfolder1\testfile2.txt”.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/skjdfjhgkjr.png" data-lazy- height="190" src="data:image/svg xml,” width=”856″>

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.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/Delete-Files-in-PowerShell-7.png" data-lazy- height="226" src="data:image/svg xml,” width=”843″>

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

<img alt="" data-del="avatar" data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/talha-150×150.jpg61132db88fea7.jpg" height="112" src="data:image/svg xml,” width=”112″>

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.