Powershell delete directory

Created at 22-Dec-2023 , By samar


In PowerShell, you can use the Remove-Item cmdlet to delete a directory. You have to simply use Remove-Item cmdlet in your terminal with the actual path of the directory you want to delete.

 

Remove-Item Command to delete directory

Remove-Item 'D:\Folder1'

The above command will delete the Folder1 Directory from the D drive of your windows.

 

Remove-Item command with –Force parameter

Remove-Item 'D:\Folder1' –Force

Be careful while using the Remove-Item cmdlet with the -Force parameter, because it will delete the directory without asking for any confirmation, and data will be permanently lost.

 

Remove-Item command with -Recurse parameter

Remove-Item 'D:\Folder1' –Recurse

It will remove items in the specified directory and its subdirectories recursively. It will delete the Folder1 and inner directory (directory inside Folder1 directory) as well.

 

The Remove-Item cmdlet in Windows PowerShell has several parameters that you can use to customize its behavior. Here are some of the important parameters written down:


-Path: Specifies the path of the directory that you want to delete.

Remove-Item -Path "D:\Folder1"

 

-Include: Specifies an array of strings that determines which items have to be included for delete. Here we will get the items that we want to delete using include parameter and delete it using Remove-Item with -Force parameter after pipe (|) . 

Get-ChildItem -Path "D:\Folder1" -Include "*.txt", "*.log" | Remove-Item -Force

 

-Exclude:  Determines which items will be excluded from being deleted.

 

If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.

Buy Me A Coffee

Don't forget to share this article! Help us spread the word by clicking the share button below.

We appreciate your support and are committed to providing you valuable and informative content.

We are thankful for your never ending support.