| by Arround The Web | No comments

Delete files in PowerShell

PowerShell is Microsoft’s utility used for the automation of tasks and configuration management of Windows. It is a fully featured advanced version of Command Prompt. PowerShell is also used for the performing different file related operations, such as file creation and modification. More specifically, files can be deleted from the computer using specific cmdlets.

This tutorial will observe several methods to delete files in PowerShell.

How to Delete Files in PowerShell?

These are the given approaches to solve the stated query:

Method 1: Delete File Using the “rm” Command

rm” is a Unix-based command used to delete or remove files from certain directories. However, it can be utilized in Windows using PowerShell. The files deleted using the “rm” command do not move into the trash. Instead, they have been removed permanently from the system. So, as a Windows user, be careful while using this command.

Example 1:Use the “rm” Command in PowerShell to Delete a File

First of all, write out the “rm” command, add the “-fo” options to delete the read-only file forcefully, and specify the file path:

> rm -fo C:\Doc\File.txt

The error-free output signifies that the specified file has been deleted successfully.

Example 2: Use the “rm” Command to Delete Multiple Files

In case you want to delete multiple files from a specific directory or folder, place the Dot(.) enclosed within two wildcards, just like *.*, at the end of the directory address. As a result, all the files from the mentioned folder will get deleted:

> rm -fo C:\Doc\*.*

Example 3: How to Delete a File With Specific File Extension Using “rm” Command?

For instance, let’s delete files with the “txt” extension. To do so, place the extension with a wild character at the start:

> rm -fo C:\Doc\ -i *.txt

Method 2: Delete Files Using the “remove-item” Command

The “remove-item” command is used to remove or delete one or more than one files from a specific directory. Using this command, users can also delete files from the subfolders. Moreover, files with selective extensions can also be removed.

Example 1: Use the “remove-item” Command in PowerShell to Delete a File

Run the given command to delete a file single file:

> remove-item -fo C:\Doc\File.txt

Here, the “-fo” flag is used to force the action to happen. Additionally, it is utilized to delete read-only files:

Example 2: Use the “remove-item” Command to Delete Multiple Files

Write and execute the given command in PowerShell to delete multiple files:

> remove-item -fo C:\Doc\*.*

In the above command, *.* is added in the file path to select all the files from the folder for deletion:

Example 3: How to Delete Files With Specific Extension Using “remove-item” Command?

As discussed earlier, utilize the same format for deleting the files with the specific extension, such as “txt”:

> remove-item -fo C:\Doc\ *.txt

That was all about deleting files in PowerShell.

Conclusion

In PowerShell, files can be deleted using the “remove-item” and “rm” cmdlets. These cmdlets are very helpful while deleting read-only files. You can also delete single and multiple files, files in the sub-folders, and files with specific extensions. This tutorial has presented several different approaches for deleting files in PowerShell.

Share Button

Source: linuxhint.com

Leave a Reply