How to Delete a Folder in PowerShell?
Requirement: Remove a Directory in PowerShell.
PowerShell to Delete a Folder
Deleting a folder using PowerShell is a simple process that can be completed using a few different methods. Let’s take a look at how to delete a folder using PowerShell, step by step.
Table of contents
To begin, open the Windows Start menu, type “PowerShell” into the search bar, and open it as administrator (Otherwise, You may run into the “access to the path is denied” error!). To delete a folder in PowerShell, you can use the Remove-Item
cmdlet with the -Recurse
parameter. This cmdlet will need to specify the path of the folder you want to delete using the -Path
parameter. Here’s an example of how to use it:
Remove-Item -Path C:\path\folder -Recurse
This will delete the folder and all of its contents, including subfolders and files. You can verify that everything was deleted by going to your original folder location using Windows File explorer and making sure that it has been removed completely from there.
How to force delete a folder in PowerShell?
You can use the -Force
parameter to delete the folder and its contents without prompting for confirmation. For example:
Remove-Item -Path C:\path\folder -Recurse -Force
This will delete the folder and its contents without prompting for confirmation.
Delete a Folder if exists in PowerShell
Here is an example of how to delete a folder in PowerShell if it exists:
#Folder Path
$FolderPath = "C:\Temp\New"
#Check if folder exists
If (Test-Path $FolderPath) {
# Folder not exist, delete it!
Remove-Item -Path $FolderPath -Recurse
Write-host "Folder Deleted at '$FolderPath'!" -f Green
}
Else {
Write-host "Folder '$FolderPath' does not exists!" -f Red
}
Delete a Folder in the Current Directory with PowerShell
Once you have opened PowerShell, use the cd command to change directories to where the folder you want to delete is located. For example, if your folder is called “NewFolder” and it is located in “C:\Users\YourUserName\Documents”, then use the following command: cd C:\Users\YourUserName\Documents.
You can also use the rd
or rmdir
command, which is an alias for Remove-Item
, to delete a folder and its contents. For example, to delete a Folder “New” in the current directory with all its contents without prompting for confirmation, use:
rd .\New -Recurse -Force
Delete all Files and Sub-Folders from a Folder using PowerShell
How to delete all files and sub-folders from a Folder without deleting the given folder using PowerShell?
To delete all files and folders from a specific folder using PowerShell, you can use the Remove-Item
cmdlet with the -Recurse
and -Force
parameters.
Here’s an example of how to use this cmdlet to delete all files and folders from a folder called “C:\Temp”:
Remove-Item C:\Temp\* -Recurse -Force
The *
wildcard tells PowerShell to delete all files and folders in the “C:\Temp” folder. The -Recurse
parameter tells PowerShell to delete the contents of the folder, including any subfolders and files. The -Force
parameter tells PowerShell to delete the files and folders without prompting for confirmation.
Wrapping up
Deleting a folder using PowerShell is a simple process that can be completed using a few different methods. This article showed you how to delete a folder using PowerShell. Please note, all these methods completely remove the Folder without sending it to the recycle bin.