If you cannot 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 the deletion of a file and delete all the elements inside a folder. We have seen how to delete files and folders using the command prompt. Now let's see how to do this 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 the Remove-item command.
Here is the tutorial in detailed form.
Delete a single file using PowerShell
To start, you need to open PowerShell. To do this, you can press Win + X and select Windows PowerShell from the list. You can also search for it in the search box on the taskbar. After that enter the following command-
Remove-item file-path
This is the basic form of the command.
Suppose you have a file named TWC.png in the TWC folder on your desktop. To remove it using Windows PowerShell, you need to enter this command-
Remove-item C:Usersuser-nameDesktopTWCTWC.png
You must include the file extension in the order. Otherwise, it will not recognize your order and a welcome message will greet you.
Delete a single folder using PowerShell
If you want to delete a folder using Windows PowerShell, you need to enter the command as mentioned before –
Remove-Item folder-path
Suppose you have a folder named TWC on your desktop. If you want to delete it, you must enter this command-
Remove-item C:Usersuser-nameDesktopTWC
If the folder is empty, it will be deleted immediately. However, if the folder contains files, you must confirm the deletion by pressing Yes and Enter buttons.
Delete multiple files using PowerShell
If you have multiple files to delete, you must enter another command. The command looks the same, but there is a little difference.
Remove-item file-path, file-path1, file-path2
You must enter all file paths like this. For example, you have a file named TWC.png on the desktop and another file named TWC1.txt in the Downloads folder. To delete them immediately, you can enter the following command-
Remove-item C:Usersuser-nameDesktopTWC.png, C:Usersuser-nameDownloadsTWC1.txt
Delete multiple folders using PowerShell
As with deleting multiple files, the command is the same to delete multiple folders using Windows PowerShell. For example, you can use a command like this-
Remove-item C:Usersuser-nameDesktopTWC, C:Usersuser-nameDownloadsTWC1
This command will delete the TWC folder from the desktop and the TWC1 folder from the Downloads folder at the same time.
For your information, if you use these commands, your files and folders will be permanently deleted. In other words, you can't find them in the trash.
Check items in a folder
In case you can't open a folder, but want to know 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 everything –
Get-ChildItem C:Usersuser-nameDesktopTWC
By default, it displays the mode, LastWriteTime, length and name. You can get the filename from here so you can delete the correct item.
Check the time of the last modification and the creation time
If you want to check the time when a file was created or modified, you can use this command-
Get-ChildItem C:Usersuser-nameDesktopTWC | 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 contains read-only or hidden files that you want to delete, you cannot use the command mentioned above, as it will display an error. In this case, you must use the -Force parameter. For example, if there are hidden or read-only files in the TWC folder which is placed on the desktop, you should enter this command-
Remove-item C:Usersuser-nameDesktopTWC -force
The same command can also be used to delete a read-only file. In both cases, you will receive a confirmation message where you will have to type Yes and press the Enter button.
Delete without confirmation
If you are using the very first order of this item, you will be greeted with a confirmation message. However, if you don't want to get such a message, you can use a parameter called -recurse.
For example,
Remove-item C:Usersuser-nameDesktopTWC -recurse
After entering this command, your folder or file will be permanently deleted without any confirmation.
Exclude certain file types
Suppose you have several types of files such as TXT, PNG, DOCX, PDF, etc. and want to delete everything except TXT files. In this case, you can use the -exclude parameter. The command looks like this-
Remove-Item –path C:Usersuser-nameDesktopTWC* -exclude *.txt
You may have guessed it already – this will exclude all .txt files and delete others. However, it displays a confirmation message. If you want to delete them directly, you can use the -recurse as previously mentioned.
I hope you find these commands useful.