Updated March 6, 2023
Introduction to PowerShell Delete Folder
PowerShell folder delete operation is to remove the folder from the specified location, whether its local path or the shared path using the cmdlet Remove-Item or other .Net approach, which performs to delete the folders or the subfolders and their contents and uses the specific switches to deal with the different types of the folder attributes like Read-only, hidden, etc to remove them.
Syntax of PowerShell Delete Folder
PowerShell uses various methods to delete the folders from the Windows operating system.
1. PowerShell Remove-Item Method
Syntax:
Remove-Item
[-Path] <String[]>
[-Filter <String>]
[-Include <String[]>]
[-Exclude <String[]>]
[-Recurse]
[-Force]
[-Credential <PSCredential>]
[-WhatIf]
[-Confirm]
[-Stream <String[]>]
[<CommonParameters>]
Remove-Item
-LiteralPath <String[]>
[-Filter <String>]
[-Include <String[]>]
[-Exclude <String[]>]
[-Recurse]
[-Force]
[-Credential <PSCredential>]
[-WhatIf]
[-Confirm]
[-Stream <String[]>]
[<CommonParameters>]
From the above sets, you can use the combination of one only. You can’t use -Path and -LiteralPath both in the same command.
2. Command Prompt
- Using rmdir command
rmdir [<drive>:]<path> [/s [/q]]
- Using del
del [/p] [/f] [/s] [/q] [/a[:]<attributes>] <names>
erase [/p] [/f] [/s] [/q] [/a[:]<attributes>] <names>
3. FileSystem Object Method
Using the FileSystem Object Method. We can create a file system object and use the DeleteFolder method.
object.DeleteFolder folderspec, [ force ]
4. Using .Net Class Approach
[System.IO.Directory] .Net class uses the method Delete() folder.[System.IO.Directory]::Delete(String)
[System.IO.Directory]::Delete(String,Boolean)
How to Delete Folders in the PowerShell?
As shown in the syntax, PowerShell uses various methods like Native command Remove-Item, cmd commands rmdir and del, File System Object method, and .Net approach to delete the files from the folder.
Window System folder has various attributes associated with it like Read-Only, Hidden, Archive, etc, and to delete them we may need the various parameters to pass in the cmdlet.
Examples to Implement of PowerShell Delete Folder
Below we discuss examples of PowerShell Delete Folder:
Example #1
Remove-Item to delete the folder with Subfolders and files
Consider we have a folder stored at the location C:\temp\Test\ and we need to delete that folder using the Remove-Item command. The folder contains the below items.
Command:
Get-ChildItem C:\Temp\Test\
Output:
To remove the items,
Remove-Item C:\Temp\Test\ -Verbose
Once you run this command, it will ask for confirmation.
You can press ‘Yes’ every time or ‘Yes to All’ to confirm all the files and folders.
This prompt is generated because the Test folder contains the subfolders and files, use the -Recurse Parameter to delete all the inside files and folders and -Force parameter to delete them forcefully.
Remove-Item C:\Temp\Test\ -Recurse -Force -Verbose
Output:
Example #2
Use -WhatIF parameter With Remove-Item to avert wrong folder deletion. We can use -WhatIf parameter that shows which files and folders will get deleted to avert any wrong folder deletion. See the example below.
Remove-Item C:\Temp\Test\ -Recurse -Force -WhatIf -Verbose
Output:
If you have permission to delete the remote folder then you can provide the shared path of the folder there. For example,
Remove-Item '\\Computer1\C$\Temp\Test\' -Recurse -Force -WhatIf -Verbose
The above command will delete the C:\temp\Test folder on the remote computer Computer1.
Example #3
Use Remove-Item as the pipeline. We can retrieve the files and folder content with the Get-ChildItem and Remove-Item to pipeline the output of the first command to remove the folder.
Get-ChildItem C:\Temp\Test\ | Remove-Item -Recurse -Force -Verbose
The problem with this approach is that it deletes the content of the folder but not the folder itself. You need to add more logic to delete but if you want to delete the specific subfolder inside the parent folder then this method is useful that we will see in the example8.
Example #4
Using cmd commands to delete the folder.
- Using del
When we use the Del command it only deletes the inside files and folders and but leaves the original folder empty.
del c:\Temp\Test\
When you run this command to delete the test folder content, it will prompt for deletion.
To delete the subfolders, files including read-only, forcefully use the below command.
c:\>del c:\Temp\Test /s /q /f
Output:
- Use the rmdir to delete the folder instead of the del
rmdir C:\Temp\Test
when you run the above command, you will get the message as below because the directory contains the subfolders and files.
Use the below command to delete the specified folder, subfolders, and files in quiet mode.
c:\>rmdir C:\Temp\Test /s /q
Example #5
Using File System Object method. We can also delete the folder using the ComObject FileSystemObject as shown below.
$obj = New-Object -ComObject Scripting.FileSystemObject
$obj.DeleteFolder("C:\Temp\Test")
Example #6
Using .Net Class method. We can use the .Net class System.IO.Directory with the Delete() method to delete the folder.
For example:
PS C:\> [System.IO.Directory]::Delete("C:\Temp\Test")
When you run the above command, it throws an exception that the directory is not empty because the above command is meant to delete the empty directory.
So to delete the folder, add the second parameter $true for deleting the folder which is not empty.
[System.IO.Directory]::Delete("C:\Temp\Test", $true)
Example #7
Using PowerShell DSC to delete the folder. Using the declarative method DSC to delete the folder.
Configuration FolderDelete{
Node Localhost{
File TestFolderDelete{
DestinationPath = 'C:\Temp\Test'
Type = 'Directory'
Ensure = 'Absent'
Force = $true
}
}
}
FolderDelete -OutputPath C:\Temp\FolderDelete\ -Verbose
Start-DscConfiguration -Path C:\Temp\FolderDelete -Wait -Force -Verbose
Output:
The above command will store the configuration in C:\temp\FolderDelete, and make sure that the path exists before you run the script and it will generate the MOF file.
You can also provide the remote nodes in the Node section like
Node @("Computer1","Computer2")
Example #8
Delete the hidden folders using PowerShell. To remove the hidden folders we first need to retrieve the hidden folder using Get-ChildItem.
Get-ChildItem C:\Temp\Test\ -Hidden
Output:
We will append Remove-Item as a Pipeline to delete that hidden folder.
Get-ChildItem C:\Temp\Test\ -Hidden | Remove-Item -Force -Recurse -Verbose
Output:
Conclusion
Windows File Explorer is used to interact with the file system files and folders but for the various reasons we need to delete the folder when we write a script or for the regular maintenance of the system we need to delete the folders to free up the extra space, we can use the methods described in this article.
Recommended Articles
This is a guide to PowerShell Delete Folder. Here we discuss a brief overview on the PowerShell Delete Folder, its syntax, methods and its Examples along with Code Implementation. You may also have a look at the following articles to learn more–