Modify the contents of a file in powershell

2019-08-12 06:40发布

I have a situation where I need to modify the contents of a file using PowerShell. The string already there will be similar to ToolsVersion="3.5". However I can not be positive that "3.5" will always be what is inside the quotes. I need to change the value inside the quotes to "12.0". Is there a way to find the "ToolsVersion=" in the file, then modify the value inside the quotes that follows it?

1条回答
狗以群分
2楼-- · 2019-08-12 07:40

You can use Get-Content, -replace, and Out-File:

(Get-Content C:\path\to\file.txt) -replace 'ToolsVersion=".*?"', 'ToolsVersion="12.0"' | Out-File C:\path\to\file.txt
查看更多
登录 后发表回答