If you are unable to delete a file or folder from your computer, you can use Windows PowerShell to delete any file and folder effortlessly. The advantage of using PowerShell is that you can force delete a file and remove all items from inside a folder. We have seen how to delete files & folders using Command Prompt. Now let’s see how to do it using PowerShell.
Use PowerShell to delete files and folders
To delete files and folders using Windows PowerShell, follow these steps-
- Open Windows PowerShell
- Identify the folder
- Use Remove-item command.
Here is the tutorial in detail form.
Delete a single file using PowerShell
To get started, you need to open PowerShell. For that, you can press Win+X, and select Windows PowerShell from the list. Alternatively, you can search for it in the Taskbar search box. After that, enter the following command-
Remove-item file-path
This is the basic form of the command.
Let’s assume that you have a file named TWC.png in the TWC folder on your Desktop. To delete it using Windows PowerShell, you need to enter this command-
Remove-item C:\Users\user-name\Desktop\TWC\TWC.png
You have to include the file extension in the command. Otherwise, it won’t recognize your command, and a welcome message will welcome you.
Related: How to delete Files older than X days in Windows
Delete a single folder using PowerShell
If you want to delete a folder using Windows PowerShell, you need to enter the command as mentioned earlier-
Remove-Item folder-path
Let’s assume that you have a folder named TWC on your Desktop. If you want to delete it, you need to enter this command-
Remove-item C:\Users\user-name\Desktop\TWC
If the folder is empty, it will be deleted immediately. However, if the folder has some files, you need to confirm the delete by pressing Y and Enter buttons.
Delete multiple files using PowerShell
If you have multiple files to delete, you need to enter a different command. The command looks the same, but there is a small difference.
Remove-item file-path, file-path1, file-path2
You need to enter all the file paths like this. For example, you have one file named TWC.png on the Desktop and another file named TWC1.txt in the Downloads folder. To delete them at once, you can enter the following command-
Remove-item C:\Users\user-name\Desktop\TWC.png, C:\Users\user-name\Downloads\TWC1.txt
Delete multiple folders using PowerShell
Like deleting multiple files, the command is the same to remove more than one folder using Windows PowerShell. For example, you can use a command like this-
Remove-item C:\Users\user-name\Desktop\TWC, C:\Users\user-name\Downloads\TWC1
This command will delete the TWC folder from the Desktop and TWC1 folder from the Downloads folder at once.
For your information, if you use these commands, your files and folders will be deleted permanently. In other words, you cannot find them in the Recycle Bin.
Check items inside a folder
In case you cannot open a folder, but you want to know about the items, you can use this command-
Get-ChildItem folder-path
If you have a folder on your Desktop named TWC, you need to enter the following command to reveal all things-
Get-ChildItem C:\Users\user-name\Desktop\TWC
By default, it shows the Mode, LastWriteTime, Length, and Name. You can get the file name from here so that you can delete the correct item.
Check last modification time and creation time
If you want to check the time when a file was created or modified, you can use this command-
Get-ChildItem C:\Users\user-name\Desktop\TWC | Select-Object FullName, LastWriteTime, CreationTime
You can find three columns with all the file names and other details.
Force delete an item
If your folder has some read-only or hidden files that you want to remove, you cannot use the command mentioned above, as it will show an error. In this case, you have to use the -force parameter. For example, if there are some hidden or read-only files in the TWC folder that is placed on the Desktop, you have to enter this command-
Remove-item C:\Users\user-name\Desktop\TWC -force
The same command can be used to delete a read-only file, as well. In either case, you will get a confirmation message where you need to type Y and press the Enter button.
Delete without confirmation
If you use the very first command of this article, you will be greeted with a confirmation message. However, if you do not want to get such a message, you can use a parameter called -recurse.
For example,
Remove-item C:\Users\user-name\Desktop\TWC -recurse
After entering this command, your folder or file will be deleted permanently without any confirmation.
Exclude certain file type
Let’s assume that you have multiple file types such as TXT, PNG, DOCX, PDF, etc. and you want to delete all items except the TXT files. In that case, you can use the -exclude parameter. The command looks like this-
Remove-Item –path C:\Users\user-name\Desktop\TWC* -exclude *.txt
You might have already guessed – it will exclude all the .txt files and delete others. However, it shows a confirmation message. If you want to delete them directly, you can use the -recurse parameter as mentioned earlier.
I hope these commands will be helpful for you.