希望通过csv文件使用PowerShell进行查找和替换



遇到了一个障碍,试图使用csv中列出的文件查找和替换字符串。举个例子。如有任何帮助,我将不胜感激。

$inputfiles = Import-Csv D:temptestfoldertest.csv
$find = 'shazaam'
$replace ='shazaam1234'
foreach ($inputfile in $inputfiles) {
(Get-Content $PSItem.FullName) -Replace $oldString,$newString | Set-Content -Path 
$PSItem.FullName
}

如果csv的第一行是" Fullname ",那么下面的脚本应该可以工作。

# Import files with import csv (if first line isn't 'FullName' use Get-Content instead)
$inputfiles = Import-Csv D:temptestfoldertest.csv
$find = 'shazaam'
$replace ='shazaam1234'
# iterate through inputfiles
$inputfiles.Foreach({
# use .Replace instead because it uses no regular expressions
(Get-Content -Path $_.FullName).Replace($find,$replace) | Set-Content -Path $_.FullName
})

最新更新