首页 > 解决方案 > 如何在 PowerShell 中为多个文件运行命令

问题描述

我有一个包含图像的文件夹,我需要使用 ImageMagick 转换它们。如何在 Powershell 中为每个文件(作为参数)运行命令?

标签: powershellimage-processing

解决方案


返回一个FileInfoGet-ChildItem对象数组,您不应将它们视为包含路径和文件名的简单字符串。 相反,使用这些对象的属性和
FullNameName

Get-ChildItem -Path "C:\Pictres" -File | ForEach-Object {
    # The automatic variable '$_' or '$PSItem' contains the current object in the PowerShell pipeline.
    $originalImage  = $_.FullName
    $convertedImage = Join-Path -Path $_.DirectoryName -ChildPath ('test_{0}' -f $_.Name)
    & 'C:\INSTALL\ImageMagick-7.0.8-Q16\magick.exe' "$originalImage" -negate "$convertedImage"
}

推荐阅读