Powershell使用正则表达式替换文件

nag*_*hal 2 regex powershell replace file gpo

下午好,我希望修改首选项文件,以使用稍后将由 gpo 部署的 powershell 脚本重置 chrome 中的默认缩放。

\n\n

该策略是删除负责默认缩放的部分设置。手工完成时效果非常好。但是,使用此脚本,我收到一条消息,表明首选项文件损坏。

\n\n

代码:

\n\n
#D\xc3\xa9clarations\n$preferencePath = Join-Path $env:USERPROFILE                      \'AppData\\Local\\Google\\Chrome\\User Data\\Default\\Preferences\'\n$newPreferencePath = Join-Path $env:USERPROFILE   \'AppData\\Local\\Google\\Chrome\\User Data\\Default\\newpref.txt\'\n$regex = \'("default_zoom_level":)[^\\s]\\d*.\\d*(":)\\d*.\\d*(},)\'\n\n#Cr\xc3\xa9er la version corrig\xc3\xa9 du fichier de pr\xc3\xa9f\xc3\xa9rence\nGet-Content -path $preferencePath | % { $_ -Replace $regex , \'\' }  |  Out-    File $newPreferencePath\n\n#Renomme les fichiers pour que le bon soit pris en compte\nRename-Item $preferencePath "PreferencesBAK"\nRename-Item $newPreferencePath "Preferences"\n
Run Code Online (Sandbox Code Playgroud)\n\n

有人可以解释该代码有什么问题吗?

\n\n

谢谢你!

\n

Joe*_*oey 5

请注意,Out-File默认使用 UTF-16LE 作为文件编码。您很可能需要将其写为 UTF-8(事先检查文件的编码):

... | Out-File -Encoding UTF8 $newPreferencePath
Run Code Online (Sandbox Code Playgroud)