PowerShell modifies a string in a file (-replace operator)

Source: Internet
Author: User

Using the-replace operator

test.txt File contents

1234
Hello World
AAA "HHH"
Fwdbz

Test.ps1 script File

$ (get-content E:\demo\test.txt) |
Foreach-object {$_-replace ("1234", "5678")} |
Foreach-object {$_-replace ("Hello World", "Hello java")} |
Out-file E:\demo\test.txt

This is a simple string substitution, replacing 1234 in the Test.txt file with 5678, "Hello World" replaced by "Hello Java"

Above is a parameterless script execution that modifies the string in the file

The following is a string in the Pass parameter modification file

Test1.ps1 script File

#执行操作 TEST1.PS1 1234 5678 parameters are separated by a space, and the result of the operation is to replace 1234 in the Test.txt file with 5678

#输出参数1, 2

Write-host "$ ($args [0])" "$ ($args [2])"

$p 1 = "$ ($args [0])"
$p 2 = "$ ($args [1])"
$ (get-content E:\demo\test.txt) |
Foreach-object {$_-replace ($p 1, $p 2)} |
Out-file E:\demo\test.txt

PowerShell modifies a string in a file (-replace operator)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.