Home > Net >  Delete content of a folder using powershell
Delete content of a folder using powershell

Time:04-08

I am having below code to delete content of a folder.

$Folder = "C:\Scripts\"
Remove-Item –path $Folder –recurse -Force
write-host "Content of $Folder Deleted.." -BackgroundColor Green -ForegroundColor Black

It is working fine, but it also deleting the parent folder (Scripts) also.

Please let me know what I am missing here

CodePudding user response:

With your command you are deleting the folder. If you want to delete the content of the folder you need to first get the contents and pipe them into the remove-item command. Updated code looks like this:

$Folder = "C:\Scripts\"
Get-ChildItem $Folder | Remove-Item –recurse -Force
write-host "Content of $Folder Deleted.." -BackgroundColor Green -ForegroundColor Black
  • Related