How to easily delete files and folders using Windows PowerShell
If you cannot delete some files or folders from your Windows computer, then we recommend that you use such a useful gizmo as PowerShell. With this tool, you can easily delete files and folders from the system that are difficult or impossible to delete using traditional methods.
PowerShell to delete files and folders
To remove files and folders from the system using Windows PowerShell, you need to follow these steps:
- open Windows PowerShell;
- find the file or folder you want;
- run the command to delete the object.
That’s right, it’s that simple. However, let’s take a closer look at the PowerShell uninstallation process.
Deleting a single file via PowerShell
First of all, you need to open a PowerShell window in front of you. To do this, press the Windows + S combination on the keyboard, and then write a PowerShell request. Right click on the found result and select “Run as administrator”.
To delete a specific file, you need to run the following command:
Remove-item [путь к файлу]
In general, a fairly simple command. Let’s assume you have a folder on your Desktop called “Testfiles” and inside it is a Testfiles.jpg file that you need to delete. In this case, the above command will look like this:
Remove-item C: Users [имя пользователя] Desktop Testfiles Testfiles.jpg
Be sure to remember to write the file extension for deletion, otherwise PowerShell simply won’t parse the command – the file will remain on the computer and you will see an error in the tool window.
Deleting one folder via PowerShell
Want to delete a specific folder in your Windows OS? Then you need to use exactly the same command as before.
Remove-item [путь к папке]
Let’s assume you have a folder called “Testfiles” on your Desktop – you want to get rid of it. In this case, the command will take the following form:
Remove-item C: Users [имя пользователя] Desktop Testfiles
If the folder is empty, it will be deleted immediately. Nevertheless, if there are at least some files in it, you will have to press the Y and Enter buttons to confirm their deletion. In the PowerShell window there will be hints about what buttons are responsible for.
Delete multiple files via PowerShell
If you want to get rid of several files at once through PowerShell, then you will need to use a slightly different command. It will look like this:
Remove-item [путь к файлу], [путь к файлу1], [путь к файлу2]
That’s right, you will have to enter a location for each file that you want to remove from your system. Suppose you have AnotherTest.txt file on your desktop and AnotherTest1.jpg in your Downloads folder. What if both need to be removed? In this case, the command for PowerShell will look like this:
Remove-item C: Users [имя пользователя] Desktop AnotherTest.txt, C: Users [имя пользователя] Downloads AnotherTest1.jpg
Removing multiple folders via PowerShell
The command for deleting multiple folders is not much different from the command for deleting multiple files. Using the previous examples, a PowerShell command for multiple folders would look like this:
Remove-item C: Users [имя пользователя] Desktop AnotherTest, C: Users [имя пользователя] Downloads AnotherTest1
Again, if any files will be located in these folders, then you will need to confirm their deletion in the PowerShell window by pressing Y and Enter. In addition, it is worth noting that files and folders deleted in this way do not end up in the Trash on the Desktop, but are immediately deleted from the system.
Checking for files in a folder via PowerShell
If you cannot open a folder, but want to know what is in it, you can use the following command:
Get-ChildItem [путь к папке]
For example, if you want to look inside the “AnotherTest2” folder located on the Desktop, the command will look like this:
Get-ChildItem C: Users [имя пользователя] Desktop AnotherTest2
By using this command, you will see several columns, which will contain useful information regarding the files inside the folder you need. The last column (by default) will be responsible for the name of the files. This way you will know whether to delete this folder or not.
Checking Last Modified and Created Time via PowerShell
If you want to understand when an object (folder or file) was created and modified on your system, then you need to run the following command in PowerShell:
Get-ChildItem C: Users [имя пользователя] Desktop AnotherTest2 | Select-Object FullName, LastWriteTime, CreationTime
Force deletion of an object via PowerShell
You cannot use the above commands to remove hidden and read-only files. If you try to do this, you will be greeted with an error in the PowerShell window. However, you can forcibly get rid of certain files / folders by using the -force parameter. Suppose you have anotherTest3 folder on your Desktop with hidden files inside and you want to get rid of it. To delete this folder, you need to run the following command in PowerShell:
Remove-item C: Users [имя пользователя] Desktop AnotherTest3 -force
As always, if you want to delete two or more objects, you will need to press Y and Enter in the PowerShell window to confirm your intentions.
Delete files / folders without confirmation via PowerShell
By the way, speaking of confirmation of deletion, don’t you want to constantly deal with them? Then you can skip these requests by applying a special parameter to the delete command. Such a command will look like this:
Remove-item C: Users [имя пользователя] Desktop AnotherTest3 -recurse
After entering this command, all specified files and folders will be immediately deleted from your computer – you do not need to confirm anything.
Excluding specific files from the PowerShell delete command
Let’s pretend you have several files with different extensions like TXT, PNG, DOCX, PDF, and so on. You want to remove everything except files with the TXT extension. In this case, you need to use the -exclude parameter along with the corresponding command. Here’s what you should get:
Remove-Item –path C: Users [имя пользователя] Desktop AnotherTest3 * -exclude * .txt
As you might have guessed, absolutely all objects will be deleted from the AnotherTest3 folder, except for text files. A very handy command for cleaning up a folder with a lot of different files mixed in, and you need to filter it, getting rid of all unnecessary.