We help IT Professionals succeed at work.

PowerShell -replace

Robert Pollick
on
I am attempting to replace part of a string and replace it with another.

Here is my string:
2/6/2020 3:46:22 PM - Server Name                              =>HHHHHHHHHAA01.nidy.state.xx.us
I want to replace, "2/6/2020 3:46:22 PM - Server Name" with "Server Name"

Below is what I have so far and I stuck

$replace_space = Get-Content C:\Temp\Environment_Verification_NGDI\UAT27_a.txt -Encoding Ascii
$replace_space -replace '(?<=Server Name=").*?(?=")','Server Name' | Set-Content C:\Temp\Environment_Verification_NGDI\UATxx_b.txt

Open in new window

Comment
Watch Question

Most Valuable Expert 2019
Most Valuable Expert 2018
Commented:
That would be the literal string "Server Name"?
So something like this?
$replace_space = Get-Content C:\Temp\Environment_Verification_NGDI\UAT27_a.txt -Encoding Ascii
$replace_space -replace -replace '^.*? - Server Name', 'Server Name' | Set-Content C:\Temp\Environment_Verification_NGDI\UATxx_b.txt

Open in new window

Robert PollickConfiguration Manager

Author

Commented:
Thank you very much, that worked!