使用 PowerShell 替换 .ini 中的一行文本
Replacing a line of text in .ini using PowerShell
您好,我正在尝试 运行 PowerShell ISE 中的脚本来替换 .ini 文件中的一行文本。
(Get-Content C:\ProgramData\Nuance\NaturallySpeaking12\nssystem.ini) | ForEach-Object { $_ -replace "Default NAS Address=","Default NAS Address=BHPAPPDGN01V" } | Set-Content C:\ProgramData\Nuance\NaturallySpeaking12\nssystem.ini
此脚本有效,但是当我 运行 它多次将 .ini 文本添加到该行的末尾时,给我带来了一堆垃圾。
Example: Ran script 4 times "Default NAS
Address=BHPAPPDGN01VBHPAPPDGN01VBHPAPPDGN01VBHPAPPDGN01V"
试试这个:
(Get-Content C:\ProgramData\Nuance\NaturallySpeaking12\nssystem.ini) | ForEach-Object { $_ -replace 'Default NAS Address=.*','Default NAS Address=BHPAPPDGN01V' } | Set-Content C:\ProgramData\Nuance\NaturallySpeaking12\nssystem.ini
我只是尝试使用正则表达式 select =
后面的所有内容。
替换
"-替换 "Default NAS Address="
到
"-替换 "Default NAS Address=[A-Z0-9]*"
我不喜欢上面的建议,所以这是我的解决方案,以防有人从 Google 偶然发现它并发现它有用。
test.ini
的内容
[Other Parameter] = Something
[My Parameter] = What I wouldnt give for a change of heart
[Other Parameter] = Nothing
我的解决方案:
$MyString = Get-Content "C:\Test_Script\test.ini"
$NewString = $MyString.replace("[My Parameter] = What I wouldnt give for a change of heart","[My Parameter] = I gave my heart for a piece of string")
Set-Content -Path "C:\Test_Script\test.ini" -Value $NewString
更改了 test.ini
的内容
[Other Parameter] = Something
[My Parameter] = I gave my heart for a piece of string
[Other Parameter] = Nothing
您好,我正在尝试 运行 PowerShell ISE 中的脚本来替换 .ini 文件中的一行文本。
(Get-Content C:\ProgramData\Nuance\NaturallySpeaking12\nssystem.ini) | ForEach-Object { $_ -replace "Default NAS Address=","Default NAS Address=BHPAPPDGN01V" } | Set-Content C:\ProgramData\Nuance\NaturallySpeaking12\nssystem.ini
此脚本有效,但是当我 运行 它多次将 .ini 文本添加到该行的末尾时,给我带来了一堆垃圾。
Example: Ran script 4 times "Default NAS Address=BHPAPPDGN01VBHPAPPDGN01VBHPAPPDGN01VBHPAPPDGN01V"
试试这个:
(Get-Content C:\ProgramData\Nuance\NaturallySpeaking12\nssystem.ini) | ForEach-Object { $_ -replace 'Default NAS Address=.*','Default NAS Address=BHPAPPDGN01V' } | Set-Content C:\ProgramData\Nuance\NaturallySpeaking12\nssystem.ini
我只是尝试使用正则表达式 select =
后面的所有内容。
替换
"-替换 "Default NAS Address="
到
"-替换 "Default NAS Address=[A-Z0-9]*"
我不喜欢上面的建议,所以这是我的解决方案,以防有人从 Google 偶然发现它并发现它有用。
test.ini
的内容[Other Parameter] = Something
[My Parameter] = What I wouldnt give for a change of heart
[Other Parameter] = Nothing
我的解决方案:
$MyString = Get-Content "C:\Test_Script\test.ini"
$NewString = $MyString.replace("[My Parameter] = What I wouldnt give for a change of heart","[My Parameter] = I gave my heart for a piece of string")
Set-Content -Path "C:\Test_Script\test.ini" -Value $NewString
更改了 test.ini
的内容[Other Parameter] = Something
[My Parameter] = I gave my heart for a piece of string
[Other Parameter] = Nothing