EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 360+ Courses All in One Bundle
  • Login

PowerShell Delete Folder

Home » Data Science » Data Science Tutorials » PowerShell Tutorial » PowerShell Delete Folder

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.

Syntax of PowerShell Delete Folder

PowerShell uses various methods to delete the folders from the Windows operating system.

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

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.

Popular Course in this category
Sale
Shell Scripting Training (4 Courses, 1 Project)4 Online Courses | 1 Hands-on Project | 18+ Hours | Verifiable Certificate of Completion | Lifetime Access
4.5 (8,656 ratings)
Course Price

View Course

Related Courses
All in One Data Science Bundle (360+ Courses, 50+ projects)Data Visualization Training (15 Courses, 5+ Projects)

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

All in One Data Science Bundle (360+ Courses, 50+ projects)

360+ Online Courses

50+ projects

1500+ Hours

Verifiable Certificates

Lifetime Access

Learn More

0 Shares
Share
Tweet
Share
Primary Sidebar
PowerShell Tutorial
  • Functions
    • PowerShell Functions
    • PowerShell String Functions
    • powershell nslookup
    • PowerShell here string
    • PowerShell Wildcards
    • Regex in PowerShell
    • PowerShell not like
    • PowerShell Filter
    • PowerShell Sleep
    • PowerShell where
    • PowerShell join string
    • PowerShell Exit
    • PowerShell null
    • PowerShell Dictionary
    • PowerShell Location
    • PowerShell Trim
    • PowerShell Join-Path
    • PowerShell Execution Policy
    • PowerShell SubString
    • PowerShell Format Table
    • PowerShell Import Module
    • PowerShell ForEach Object
    • PowerShell Alias
    • PowerShell Scheduled Task
    • PowerShell Convert String to Date
    • PowerShell Split String
    • PowerShell Multiline String
    • PowerShell MultiLine Comment
    • PowerShell Rename Folder
    • PowerShell Delete Folder
    • PowerShell String Replace
    • PowerShell join
    • PowerShell xcopy
    • PowerShell Base64
    • PowerShell Tail
    • PowerShell User List
    • PowerShell remove User from group
    • PowerShell JSON Format
    • PowerShell Send Mail
    • PowerShell Convert to String
    • PowerShell Start-Process
    • PowerShell change directory
    • PowerShell Open File
    • PowerShell Batch File
    • PowerShell ZIP
    • PowerShell unzip
    • PowerShell XML
    • PowerShell XML Parsing
    • Remote PowerShell
    • PowerShell Escape Character
    • PowerShell scriptblock
    • PowerShell Executable Location
    • PowerShell Import-CSV?
    • PowerShell Export CSV
  • Basics
    • PowerShell comment
    • PowerShell Map Network Drive
    • PowerShell Append to File
    • PowerShell print
    • What is PowerShell
    • Uses Of Powershell
    • PowerShell Versions
    • How To Install PowerShell
    • PowerShell uninstall module
    • How to Use PowerShell?
    • PowerShell Logging
    • PowerShell Tools
    • PowerShell Commands
    • PowerShell Administrator
    • PowerShell Modules
    • PowerShell Registry
    • PowerShell block Comment
    • PowerShell Verbs
    • PowerShell list
    • PowerShell add user to group
    • PowerShell Write to Console
    • Variable in PowerShell
    • PowerShell New Line
    • PowerShell prompt for input
    • PowerShell File Extension
    • Powershell Remotesigned
    • PowerShell Write to File
    • PowerShell Ping
    • PowerShell wget
    • PowerShell Global variable
    • PowerShell Get-ADGroup
    • Array in PowerShell
    • PowerShell Multidimensional Array
    • PowerShell Array of Strings
    • PowerShell? join array
    • Useful PowerShell Scripts
    • String in PowerShell
    • PowerShell Switch Statement
    • PowerShell Function Parameters
    • PowerShell vs PowerShell ISE
    • PowerShell test-connection
    • PowerShell Test-NetConnection
    • PowerShell GUI
    • PowerShell Variable in String
    • PowerShell Active Directory
  • Variables
    • PowerShell Variables
    • PowerShell Environment Variables
    • PowerShell set environment variable
    • Hashtable in PowerShell
    • Set Variable in PowerShell
  • Operators
    • PowerShell Operators
    • Comparison Operators in PowerShell
    • Logical Operators in PowerShell
    • PowerShell Boolean
    • PowerShell Like Operator
  • cmdlet
    • PowerShell Wait
    • PowerShell Match
    • cmdlets in PowerShell
    • Start PowerShell from cmd
    • Add-Content in PowerShell
    • Get Help in PowerShell
    • PowerShell Copy-Item
    • PowerShell Remove-Item
    • PowerShell Move-Item
    • Get Command in PowerShell
    • PowerShell Run Command
    • Windows PowerShell ISE
    • Windows Powershell Commands
    • WinRM PowerShell
    • PowerShell Date
    • Powershell Write-Host
    • PowerShell Get-ChildItem
    • PowerShell Sort-Object
    • PowerShell Where Object
    • PowerShell Set-Content
    • PowerShell Set-Location
    • PowerShell Invoke-Command
    • PowerShell Invoke-Webrequest
    • PowerShell Get-Location
    • PowerShell Get-Date
    • PowerShell Get-Service
    • PowerShell Test-Path
    • Powershell Module Path
    • PowerShell Out-File
    • PowerShell if File Exists
    • Powershell Copy File
    • PowerShell Delete File
    • PowerShell New-Item
    • PowerShell Rename-Item
    • PowerShell ComputerName
    • PowerShell Get-Content
    • PowerShell Get-Item
    • PowerShell Get-ADUser
    • PowerShell Grep
    • PowerShell Concatenate String
    • PowerShell Get-Process
    • PowerShell Count
    • PowerShell pause
  • Control Statements
    • If Statement in PowerShell
    • If Else in PowerShell
    • Else If in PowerShell
    • Loops in PowerShell
    • For loop in PowerShell
    • PowerShell While Loop
    • PowerShell do while
    • PowerShell Loop through Array
    • PowerShell add to array
    • PowerShell ForEach Loop
    • PowerShell Break
    • PowerShell Continue
    • Switch Case in PowerShell
    • PowerShell If-Not
    • Try-catch in PowerShell
  • Interview Questions
    • PowerShell Interview Questions

Related Courses

Shell Scripting Course

All in One Data Science Courses

Data Visualization Courses

Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

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

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

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

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

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

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

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

Let’s Get Started

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

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA Login

Forgot Password?

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

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

EDUCBA

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

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

Special Offer - Shell Scripting Course Learn More