How to delete files (and Folders) with PowerShell

Configurare noua (How To)

Situatie

Solutie

Before You Begin: How to Find a File or Folder’s Full Path

To remove files or folders from your Windows PC, you’ll need the item’s full path. If you know how to get file or folder paths, skip to the relevant section below. If you aren’t sure how to copy a file or folder’s full path, we’ll show you how.

First, open a File Explorer window and locate the file or folder whose path you want to find. Then, hold down the Shift key on your keyboard, right-click your file or folder, and choose “Copy as Path.”

You’ve successfully copied the selected item’s path to your clipboard. You can now paste this path (using Ctrl+V) wherever required within the PowerShell window.

How to Delete a Specific File Using PowerShell

To remove a specific file from your PC, use PowerShell’s “Remove-Item” cmdlet.

Start by opening a PowerShell window on your PC. Here, type the following command, replace “PATH” with the full path to the item you want to delete, and press Enter:

Remove-Item PATH

As an example, to delete a file named “Old-List.txt” on your desktop, you’d run:

Remove-Item "C:\Users\username\Desktop\Old-List.txt"

Note that the command won’t ask for a confirmation before deleting your file. If you’d like the command to do that, add the “Confirm” parameter as follows:

Remove-Item "C:\Users\username\Desktop\Old-List.txt" -Confirm

How to Delete a Specific Folder Using PowerShell

You can use PowerShell’s “Remove-Item” cmdlet to remove any directory from your PC.

Note* Deleting a folder removes all the subfolders and files inside it.

To start, launch PowerShell, type the following command, replace “PATH” with your directory’s full path, and press Enter:

Remove-Item PATH

As an example, to delete a directory named “Old Files” from your desktop, you’d run:

Remove-Item "C:\Users\username\Desktop\Old Files"

How to Delete All Files in a Folder But Keep the Folder

If you want to remove all files from a folder but retain the folder, use the “Remove-Item” cmdlet as follows.

In your PowerShell window, type the following command, replace “PATH” with the full path to the folder you want to empty, add “\*.*” before the final quotation mark, and press Enter:

Remove-Item PATH\*.*

For example, to delete all files from a folder named “Your Files” from the desktop, run:

Remove-Item “C:\Users\username\Desktop\Your Files\*.*”

In this command, the first asterisk selects files with any name, and the second asterisk chooses files with any extension. This translates to selecting all the files in the specified folder.

How to Delete All Files From a Folder and Its Subfolders

If you’re looking to remove all files from a folder and its subfolders, add the “Recurse” and “Include” parameters to the “Remove-Item” cmdlet.

Open a PowerShell window, enter the following command, replace “PATH” with the full path to the folder, and press Enter:

Remove-Item PATH -Recurse -Include *.*

Here, the “Recurse” parameter ensures the subfolders’ files are deleted as well. The “Include” parameter ensures files with any name and extension are removed.

As an example, to remove all files from the “Downloads” folder and its subfolders on the desktop, run:

Remove-Item "C:\Users\username\Desktop\Downloads" -Recurse -Include *.*


How to Delete Files With Wildcards

PowerShell offers wildcards, allowing you to delete various kinds of files by just specifying those file types in your command. In all the examples below, replace “PATH” with the full path to your folder.

For example, if you want to remove all the JPG files from a folder, use the following command:

Remove-Item PATH -Include *.jpg

Another use of wildcards is to delete all but a specific file type from your directory. For example, to remove all files except for PDF files from a folder, use the following command:

Remove-Item PATH -Exclude *.pdf

Another advanced use of PowerShell is to remove all empty folders from the given directory. In this case, use the following command, replacing “PATH” with the full path to the directory:

Get-ChildItem -Recurse PATH | where { $_.PSISContainer -and @( $_ | Get-ChildItem ).Count -eq 0 } | Remove-Item

Tip solutie

Permanent
Etichetare:

Voteaza

(0 din 0 persoane apreciaza acest articol)

Despre Autor

Leave A Comment?