Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Delete content of a folder using powershell

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading