EDUCBA

EDUCBA

MENUMENU
  • Explore
    • Lifetime Membership
    • All in One Bundles
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign up
Home Data Science Data Science Tutorials PowerShell Tutorial PowerShell Delete Folder

PowerShell Delete Folder

Updated March 6, 2023

PowerShell Delete Folder

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.

ADVERTISEMENT
Popular Course in this category
WINDOWS POWERSHELL Course Bundle - 7 Courses in 1

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

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:

PowerShell Delete Folder Example 1

To remove the items,

Remove-Item C:\Temp\Test\ -Verbose

Once you run this command, it will ask for confirmation.

Confirmation Example

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:

Recurse Paramete Example 3

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:

PowerShell Delete Folder Example 2

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.

command to delete the test folder content

To delete the subfolders, files including read-only, forcefully use the below command.

c:\>del c:\Temp\Test /s /q /f

Output:

To delete the subfolders

  • 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.

subfolders and file Example 4

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.

Empty Directory Example 6

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:

PowerShell Delete Folder Example 7

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:

Get-ChildItem Example 8

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:

PowerShell Delete Folder Example 8

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–

  1. PowerShell Loop through Array
  2. PowerShell Dictionary
  3. Powershell Copy File
  4. PowerShell join string
ADVERTISEMENT
MICROSOFT POWER BI Course Bundle - 8 Courses in 1
34+ Hours of HD Videos
8 Courses
Verifiable Certificate of Completion
Lifetime Access
4.5
ADVERTISEMENT
CYBER SECURITY & ETHICAL HACKING Course Bundle - 13 Courses in 1 | 3 Mock Tests
64+ Hours of HD Videos
13 Courses
3 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5
ADVERTISEMENT
MICROSOFT AZURE Course Bundle - 15 Courses in 1 | 12 Mock Tests
63+ Hour of HD Videos
15 Courses
12 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5
ADVERTISEMENT
KALI LINUX Course Bundle - 6 Courses in 1
20+ Hours of HD Videos
6 Courses
Verifiable Certificate of Completion
Lifetime Access
4.5
Primary Sidebar
Footer
Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
  • Blog as Guest
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2023 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

🚀 Cyber Monday Reloaded Price Drop! All in One Universal Bundle (3700+ Courses) @ 🎁 90% OFF - Ends in ENROLL NOW