开发者

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜