深入浅出PowerShell——遍历文件夹中的xml文件并替换目标字符串
<Users>
<Name>$user</Name>
<Gender>Male</Gender>
</Users>
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
# D:\Services\ 指定要递归遍历查找的目录
# *.config 找查找的文件名
$fileList = Get-ChildItem 'F:\temp\test\' -recurse *.xml | %{$_.FullName}
$baishi = {$user}
Foreach($file in $fileList)
{
# $tmpXML = [xml](Get-Content $file)
# $tmpXML.users.SID
# $tmpXML.Replace("#user", "Fred")
$tmpXML = [string](Get-Content $file)
$tmpXML = $tmpXML.replace($baishi,"Fred")
Set-Content -Path $file -Value $tmpXML
}
<Users> <Name>Fred</Name> <Gender>Male</Gender> </Users>