Top 7 Ways to Delete Files or Folders With PowerShell in Windows

An important way to housekeep and maintain your computer is by deleting unwanted or unused files and folders. While there are many ways to do this, power users may prefer to delete files or folders with PowerShell. This is because it offers granular control and precision, allows for bulk operations, and allows users to bypass File Explorer limitations.

Top_N_Ways_to_Delete_Files_or_Folders_With_PowerShell_in_Windows

This guide will show you all possible options to delete files and folders using Microsoft PowerShell. Some of these solutions will be important for problematic files if you need to force delete the file. Let us get to it.

Prerequisite: Find the Full File or Folder Path

Before proceeding to delete files or folders with PowerShell, you should know the full path of the file. Follow the steps below to identify the path of a file in Windows 11.

Step 1: Right-click on the file or folder you want to delete and click Copy as path from the context menu.

copy file path

Step 2: Alternatively, click the file and press Ctrl + Shift + C. Now you have the path, and you may use it to delete all files in a folder when needed.

1. Delete a Specific File With PowerShell

You may need to delete files with a specific name. In this case, you will have to employ the Remove-Item command. This command will permanently delete the file, skipping the recycle bin. Here is how to use it.

Step 1: Press Windows + R to open the Run dialog, type PowerShell, and hit Ctrl + Shift + Enter to launch Microsoft PowerShell.

open powershell 7

Step 2: Type the command below and hit Enter, substituting file_path for the actual file path with the file name and extension, as shown in the screenshot.

See more:  Left Hand Ben Location in Tower of Fantasy

Remove-Item “file_path”

Remove Item command

Step 3: Alternatively, you may add the -Confirm parameter to your command if you need it to ask for confirmation before deleting the file.

Remove-Item “file_path” -Confirm

confirm parameter

2. Delete a Specific Folder With PowerShell

Like we did when deleting a specific file, you may also use the Remove-Item command to permanently delete a specific folder from your computer. The steps are very similar.

Step 1: Press Windows + R to open the Run dialog, type PowerShell, and hit Ctrl + Shift + Enter to launch Microsoft PowerShell.

open powershell 7

Step 2: Type the command below and hit Enter, substituting folder_path for the actual file path with the file name and extension, as shown in the screenshot.

Remove-Item “folder_path” -confirm

Delete folder with confirm

Step 3: When asked to confirm, press A and hit Enter.

Confirming delete

Step 4: Alternatively, you may skip the -confirm argument if you do not want to be asked for confirmation before deleting. Hence, the command will remain as shown below.

Remove-Item “folder_path”

Delete folder no confirmation

3. Delete Files and Subfolders From a Folder

Once again, you need the Remove-Item command to delete files and subfolders. However, this operation will require two extra parameters – Recurse and Include. We show you how they are used below.

Step 1: Press Windows + R to open the Run dialog, type PowerShell, and hit Ctrl + Shift + Enter to launch Microsoft PowerShell.

open powershell 7

Step 2: Type the command below and hit Enter, substituting item_path for the actual file path with the file name and extension, as shown in the screenshot.

Remove-Item “item_path” -Recurse -Include *.*

remove test folder

4. Delete Read-Only and Hidden Files

Deleting read-only and hidden files is impossible because they are protected from deletion by default. An attempt to delete such files will only trigger an error message, so you must force these files to be deleted by including the -Force parameter in the command. Here is how to use it.

Step 1: Press Windows + R to open the Run dialog, type PowerShell, and hit Ctrl + Shift + Enter to launch Microsoft PowerShell.

See more:  How to Fix Windows 10 Update Stuck on Checking for Updates Issue

open powershell 7

Step 2: Type the command below and hit Enter, substituting read-only_or_hidden_file_path for the actual file path with the file name and extension, as shown in the screenshot.

Remove-Item -Path “read-only_or_hidden_file_path” -Recurse -Force

Deleting hidden files

5. Deleting Files in a Folder According to Size With Powershell

Deleting files in a folder allows some flexibility, even when you do not know the actual file’s name. This is a great solution when you need to free up space by deleting files up to a certain size. Below, we show you how to delete files larger than 10 MB. In our script, we shall add comments using the # sign to explain what every line of command does.

Step 1: Press Windows + R to open the Run dialog, type PowerShell, and hit Ctrl + Shift + Enter to launch Microsoft PowerShell.

open powershell 7

Step 2: Copy and paste the command below and hit Enter, substituting file_path for your actual file path.

#Declare the location of the file$path = “file_path”#Set the size of files to be deleted$sizeInMb = 10#Convert the size to KB$size = $sizeInMb*1024*1024#Use the command below to delete all files larger than the declared file sizeGet-ChildItem -Path $path -Recurse -File | Where-Object { $_.length -gt $size } | Remove-Item

Deleting files by size

6. Deleting Files in a Folder According to Age With Powershell

One flexible deleting option PowerShell gives is deleting according to file age. So you may specify -delete files older than x days to get rid of the files within a folder that meets that criteria. This is a common practice when you need to clean directories. Below, we will show you how to delete files from a folder over ten days.

Step 1: Press Windows + R to open the Run dialog, type PowerShell, and hit Ctrl + Shift + Enter to launch Microsoft PowerShell.

open powershell 7

Step 2: Copy and paste the command below and hit Enter, substituting C:\Users\afamo\Desktop for your actual file path.

$dateTime = (Get-Date).AddDays(-10)$path = “C:\Users\afamo\Desktop”Get-ChildItem -Path $Path -Recurse -File | Where-Object { $_.LastWriteTime -lt $dateTime } | Remove-Item

Delete files by date

7. Deleting Files With the Exclude and Include Filters in PowerShell

You can have more flexibility while deleting by excluding and including specific items based on a set of criteria. To do this, you will use the Exclude and Include filters. So, for example, you may choose all files with a certain string or a portion of a string in their file name by using the Include argument. Let us show you how these filters are used.

See more:  How to Download Osu Skins

Step 1: Press Windows + R to open the Run dialog, type PowerShell, and hit Ctrl + Shift + Enter to launch Microsoft PowerShell.

open powershell 7

Step 2: Copy and paste the command below and hit Enter to remove all files with the .txt format in the specified path. Substituting -Include for -Exclude will delete all files apart from the .txt format.

Remove-Item -Path C:\Users\afamo\Desktop -Include *.txt

Step 3: You may remove all files with certain words in their names. Copy and paste the command below to remove all files with test and command in their names.

Remove-Item -Path C:\Users\afamo\Desktop -Include *test*, *command* -Recurse -WhatIf

deleting with the include argument

Deleting Files and Folders With Great Flexibility

Using a PowerShell script to delete files and folders offers excellent flexibility, as you may now know. In this guide, we have covered some of the most practical ways of doing this; you can delete files from multiple folders faster than using the traditional File Explorer.

PowerShell lets you delete a file if it exists and is indeed a robust tool. In the comment section, let us know if you could use all the solutions we explored.

Categories: How to
Source: thpttranhungdao.edu.vn/en/

Rate this post

Leave a Comment