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-
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-
You have to include the file extension in the command. Otherwise, it won’t recognize your command, and a welcome message will welcome 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 earlier-
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-
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.
You need to enter all the file paths like this. For example, you have one file named TWC.png on Desktop and another file named TWC1.txt in the Downloads folder. To delete them at once, you can enter this following command-
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-
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-
If you have a folder on your Desktop named TWC, you need to enter the following command to reveal all things-
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-
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 that 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-
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.
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-
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.
You can zip files and folders and extract zip files using the PowerShell. Here’re the commands to zip and unzip files using PowerShell in Windows.
To share multiple files or to save some disk space, you can compress them into a zip file. You can also encrypt zip files and password-protect them with just a few clicks to keep the contents from prying eyes. In Windows, there are several different ways to compress a file or folder. For example, you can right-click on a file or folder and select the “Send to → Compressed folder” to compress it. For added functionality, there are dedicated programs like 7-zip, WinRAR, etc..
Alternatively, you can also use a simple PowerShell command to zip compress files. When needed, you can use another PowerShell command to extract or unzip files. This method is particularly useful when creating custom PowerShell scripts.
In this quick guide, let me share with you the commands to zip and unzip files using PowerShell commands.
How to Zip Files using PowerShell
To compress files using PowerShell, you can use the Compress-Archive cmdlet. All you have to do is point the files you want to compress and a destination to save the zip file. Here are the exact steps.
- Open the Start menu by pressing the Start key on your keyboard.
- Type “PowerShell” and press Enter to open it.
- After opening the PowerShell window, execute the below command to zip files. Replace with the actual path of the file you want to compress and with the folder path of where you want to save the compressed file.
Compress-Archive -LiteralPath ‘ ‘ -DestinationPath ‘ ‘ - As soon as you execute the command, PowerShell will zip the file and saves it in the specified destination.
- You can close the PowerShell window.
How to Zip Multiple Files or Folder using PowerShell
If you want to compress multiple files or a folder containing files and/or sub-folders, you can do that too from the PowerShell. Here’re the steps to zip multiple files or folders using the PowerShell.
- First, put all the files you want to compress are in a single folder.
- Next, open the Start menu.
- Search for PowerShell and open it.
- In the PowerShell window, execute this command. Replace with the actual folder path you want to compress and with the destination path where you want to save the compressed zip file.
Compress-Archive -LiteralPath ‘ ‘ -DestinationPath ‘ ‘ - Depending on the folder size or number files, it can take some time to complete the compressions process.
- Once done, close the PowerShell window.
You can find the compressed zip file in the destination you set in the above command.
Extract / Unzip Files Using PowerShell
Just as you can compress files with PowerShell, you can also extract files with PowerShell’s Expand-Archive cmdlet. Just point the PowerShell to the zip file you want to extract and provide a destination directory.
- Open the PowerShell window.
- In the PowerShell, execute the below command while replacing and with the actual zip file path and destination folder path respectively.
Expand-Archive -LiteralPath -DestinationPath - Once extracted, close the PowerShell window.
You can find the extracted files in a folder in the destination path.
That is all. I hope that helps. If you are stuck or need some help, comment below and I will try to help as much as possible.