Powershell - Delete all non mp3 files
I´m totally new to Powershell and wanted to write a script that deletes all non-mp3 files in a directory.
My solution:
get-childitem -Recurse |
Where-Object {!($_.PSIsContainer)} |
Where {$_.Extension -ne ".mp3"} |
remove-item
开发者_高级运维What can be improved in this statement or could be written in another way. Are there any problems with this statement?
Thank you.
I would use just one Where-Object
command:
Get-childitem -Recurse |
Where-Object {!$_.PSIsContainer -AND $_.Extension -ne '.mp3'} |
Remove-Item -whatIf
If you're certain that no directories have 'mp3' extension :
Get-childitem -Recurse | Where-Object {$_.Extension -ne '.mp3'} |
Remove-Item -whatIf
Remove -whatIf
to delete the files.
精彩评论