在Powershell中修改文件的内容 [英] Modify the contents of a file in powershell

查看:241
本文介绍了在Powershell中修改文件的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到需要使用PowerShell修改文件内容的情况.已经存在的字符串将类似于ToolsVersion="3.5".但是,我不能肯定"3.5"始终是引号内的内容.我需要将引号内的值更改为"12.0".是否可以找到文件中的"ToolsVersion=",然后修改其后的引号内的值?

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?

推荐答案

您可以使用 Get-Content Out-File :

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

这篇关于在Powershell中修改文件的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆