Powershell

PowerShell Remove-Item cmdlet | Explained

PowerShell is a Windows built-in command-line application used for task automation and scripting purposes. It is used to perform administrative tasks with the help of a large set of cmdlets, functions, and aliases.

The PowerShell Remove-Item cmdlet is used to remove files from the system via PowerShell. You can use the file names, extensions of the files, or some file name hint to trace and remove files via PowerShell.

This write-up has covered thoroughly all the concepts of the Remove-Item cmdlet.

How to use the PowerShell Remove-Item cmdlet?

The Remove-Item cmdlet in PowerShell deletes one or more than one item from the specific storage location. It uses the file path for the purpose of deletion of the item. You can delete almost everything such as files, folders, variables, etc.

Syntax

> Remove-Item [-Path] \path\of\file [-Parameters]

The path of the file will be provided after the -Path flag. Moreover, a variety of standard parameters can be passed such as Force, Include, and Exclude whose usage is also described in the article.

Example 1: How to delete a single file using PowerShell?

To delete a single file from the specific path we will give the exact path of the file by using the “-Path” parameter to “Remove-Item”. The below example will give a demonstration of how to delete a single file in PowerShell.

> Remove-Item -Path C:\New\Docs.txt

The above line of code will only delete a single file specified by the path you have given.

Example 2: How to delete all files of a directory using PowerShell?

The Remove-Item cmdlet will utilize the wild card “*” to delete all the files located in the folder. To do so, you need to put the “*” in place of the file name and the extension.

The execution of the below command will result in the deletion of all the files in the folder path.

> Remove-Item C:\New\*.*

Example 3: How to delete specific files using PowerShell?

The parameter “-Include” is used by the “Remove-Item” cmdlet to include only the specific files in the deletion operation. The execution of the following command will result in the deletion of the files having a specific extension (.txt). The “*” wildcard will be placed at the end of the specified folder path and also before the .txt extension to remove all the “.txt” files from the directory:

> Remove-Item -Path C:\New\* -Include *.txt

The above line of code will only delete only those file extensions specified by you using PowerShell.

Moreover, you can use the “-Exclude” parameter used by the “Remove-Item” cmdlet to delete the files except for the selected file extensions.

Example 4: How to delete hidden/read-only files using PowerShell?

If you want to delete hidden and read-only files then use the command below. The path of the file is given by the “-Path” parameter and the “-Force” parameter helps to remove the hidden/read-only files.

> Remove-Item -Path C:\New\hidden-text-file.txt -Force

The above command has deleted all hidden and read-only files using PowerShell.

Here you go, you have learned the functionality of the Remove-Item cmdlet in PowerShell.

Conclusion

The PowerShell Remove-Item cmdlet is mainly used to remove or delete specific files and folders from a file path or location. The Remove-Item cmdlet is utilized to remove unwanted files or folders from the system. This article has covered all the aspects related to the Remove-Item cmdlet. The Remove-Item cmdlet supports a variety of parameters to remove files as per your requirement.

About the author

Adnan Shabbir