Even though deleting a folder is a simple action, it takes a long time to complete when you use Windows Explorer to delete large folders. While doing the same process with the help of the command line can save a lot of your precious time. That’s why knowing how to delete a folder in PowerShell is essential. Using the cmdlet Remove-Item or another PowerShell folder deletion function, you can delete a folder from a particular location, whether on the local system or a shared path. The content of folders and subfolders is deleted through a technique that uses specialized switches to cope with different folder properties such as hidden, Read-only, etc.
Methods to Delete a Folder in PowerShell
PowerShell employs various techniques to delete folders, including the native command Remove-Item, the File System Object mechanism, and the .NET class technique, rmdir and del commands. In this post, we will check each of these techniques. So let’s get this journey started!
Delete a Folder in PowerShell using Remove-Item cmdlet
In PowerShell, the Remove-Item command is utilized to delete one or more items. This command can help you delete a wide range of items, including folders, aliases, registry keys, files, variables, and functions, as it supports many providers, including Windows PowerShell.
To demonstrate the folder deletion process through PowerShell, we have created three test folders: testfolder1, testfolder2, and testfolder3. We have also placed some files and folders inside of our test folders. In the following example, we will first check out the “testfolder1” content before its deletion. PowerShell uses the “Get-ChildItem” cmdlet for this purpose.
Now, In the “Remove-Item” command, write out the path of your selected folder with the “-Verbose” option. This option will show that the folder which is intended to delete has been deleted or not.
Enter “Y” for the “Yes” option or Enter “A” to choose the “Yes to All” option.
You can also utilize the “-Force” and “-Recurse” parameters in the same command. “-Recurse” option is used to delete the inside content of the selected folder, whereas the “-Force” parameter let them delete forcefully:
To avoid accidentally deleting folders, use the “-WhatIF” option with the Remove-Item command. This option reveals which files or folders are going to be deleted.
Utilize the below-given command with your specified folder name to have a better understanding of its execution:
“Remove-Item” also provides you the facility to only delete the content of a folder and make it empty. This method is helpful when you only want to delete the subfolders of a specific folder.
In such a case, you can utilize the “Get-ChildItem” to retrieve the child item of a folder and pass it to the “Remove-Item” cmdlet using a pipe operator [“|”]. That’s how the “Remove-Item” command will delete the subfolders.
Delete a Folder in PowerShell using the .NET class method
The .NET framework utilizes the “System.IO.Directory” class and the “Delete()” function to remove a folder in PowerShell. This command will throw an exception if the specified folder is not empty:
Pass the “$true” parameter in the “Delete()” function to delete this non-empty folder:
The output declares that “testfolder1” is deleted successfully!
Delete a Folder in PowerShell using the File system object method
PowerShell also offers other ways for deleting a folder. File System Object Method is one of them. This method comprises two steps. Create a file system object in the first step, and then we will utilize the DeleteFolder() method to delete the folder of the associated object in the second step.
First of all, open up your Windows PowerShell ISE and create a new file as follows:
Add the following code in your file and save it as “testfile2.ps1” PowerShell Script.
$obj.DeleteFolder("E:\testfolder2")
Now execute this “testfile2.ps1” by pressing the “Run” button, which is highlighted in the below-given image:
All done!
Delete a Folder in PowerShell using the rmdir command
The rmdir is a popular command used by most command-line users to remove or delete directories. Want to try the rmdir command for deleting a folder? Specify your folder path in the rmdir command, and execute it in your PowerShell:
Enter “A” to permit the delete process to continue.
Delete a Folder in PowerShell using the del command
In PowerShell, the “del” command is also used for deleting the content of a folder, leaving the specified folder empty. To do so, write out the path of your folder with the “del” command and execute it in your PowerShell:
Conclusion
To interact with the system’s files and folders, we all use Windows File Explorer. However, there are times when we need to delete a folder while freeing up space, performing routine system maintenance, deleting unnecessary files, or writing a script. PowerShell provides several commands and techniques to delete a folder. We have compiled four methods that will help you delete your selected folder within a few seconds in this post. We recommend the “Remove-Item” cmdlet for deleting a folder because this command offers you many other options combined with the folder deletion process, such as viewing the folder’s content before deleting it, etc.