Hitendra Raghuwanshi
Hitendra Raghuwanshi

Reputation: 11

Delete a file or folder using PowerShell command

I want to delete the files which are in a particular folder using PowerShell commands, but when I use Remove-Item $DownloadFilePath* -recurs (where $DownloadFilePath carries the path of the folder) to delete files then it deletes the folder itself and the files which are in its parent folder. Please let me know the solution to just delete files inside the folder.

Upvotes: 1

Views: 8059

Answers (2)

DarkLite1
DarkLite1

Reputation: 14695

And if you only want to remove files but no sub folders you can use:

$DownloadFilePath = 'S:\Test\Folder'
Get-ChildItem -Path $DownloadFilePath -File -Recurse | Remove-Item

Upvotes: 1

Ansgar Wiechers
Ansgar Wiechers

Reputation: 200193

This should delete just the contents of $DownloadFilePath:

Remove-Item $DownloadFilePath\* -Recurse

Upvotes: 0

Related Questions