Powershell - 删除所有非 mp3 文件

标签 powershell

我对 Powershell 完全陌生,想要编写一个删除目录中所有非 mp3 文件的脚本。

我的解决方案:

get-childitem -Recurse | 
Where-Object {!($_.PSIsContainer)} | 
Where {$_.Extension -ne ".mp3"} | 
remove-item

此语句中有哪些可以改进的地方,或者可以用其他方式编写。 这个说法有问题吗?

谢谢。

最佳答案

我只会使用一个 Where-Object 命令:

Get-childitem -Recurse | 
    Where-Object {!$_.PSIsContainer -AND $_.Extension -ne '.mp3'} | 
    Remove-Item -whatIf  

如果您确定没有目录具有“mp3”扩展名:

Get-childitem -Recurse | Where-Object {$_.Extension -ne '.mp3'} | 
    Remove-Item -whatIf 

删除 -whatIf 以删除文件。

关于Powershell - 删除所有非 mp3 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3086861/

相关文章:

powershell - 通过 PowerShell 和 WMI 从用户名中获取用户的电子邮件地址?

powershell - New-WebServiceProxy 无法通过 NTLM 进行身份验证

powershell - 无法使用 System.IO.Compression.FileSystem.dll

powershell - 更改 hadoop 命令的 Powershell 前景色或抑制信息

powershell - 验证数组参数的模式

powershell - 如果为真则需要检查帐户是否存在如果为假则跳过创建帐户

Powershell比较两个对象属性

windows - 通过 PowerShell 加载类型库并编写 Windows Live Writer 脚本

powershell - 使用Powershell远程访问证书存储

Powershell try catch invoke-sqlcmd