1

I have a text file (test.txt) containing the below:

User : MikeyMouse
Pswd : mycrazypswd
connectionstring : serverepor:80,pass=809hjhlkusfasdf,somepropert=somevalue

I need to use PS to update the above file's key value to mask passwords as shown below with "---"

sser : MikeyMouse
pswd : my---wd
connectionstring : serverepor:80,pass=80---df,somepropert=somevalue

Any suggestions on how bet to do that?

Thanks

0

1 Answer 1

2

Using Get/Set-Content and a Regex -Replace will do the trick.

$Path = 'test.txt'
$NewContent = (Get-Content -Path $Path -Raw) -replace '(?<=Pswd : ..)(.*)(?=..\r)', '---' -replace '(?<=pass=..)(.*?)(?=..,)', '---' 
Set-Content -Path $Path -Value $NewContent

References:

.Net Regex cheat sheet

Special thanks

@olaf: For showing me the lookahead/lookbehind way.

Sign up to request clarification or add additional context in comments.

2 Comments

Hi Sage... you are a life saver. This works for most part. For the "pass=" replace, your code return only "pass=80---" instead of "pass=80---df".
@UltraGC Indeed. I fixed my answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.