2

I been trying to figure out how to do the following in PowerShell but without much luck.

I want to replace everything between messageid> AND </messageid with the current date and time.

I have started writing some code as pasted below, can someone direct me in the right path - much appreciate it.

Get-Content -path C:\test.txt -raw -replace pattern = "(?s)messageid>(.*?)</messageid"'<message>*</messageid>' (Get-Date).ToString('dddd dd-MM-yyyy HH:mm:ss') | Set-Content -path c:\test.txt
2
  • What's the actual original text? Commented Oct 13, 2020 at 2:48
  • Hey Doug, something to this effect: <MessageID>9348399343-38493-Det-100</MessageID> Commented Oct 13, 2020 at 2:55

1 Answer 1

1

This example should help you achieve your desired result.

$text = @'
Some line of text
random text <messageid> who knows </messageid> some other text
another line
'@

$text -replace '(?s)(?<=messageid>).+?(?=</messageid)',(Get-Date).ToString('dddd dd-MM-yyyy HH:mm:ss')

Some line of text
random text <messageid>Monday 12-10-2020 21:54:04</messageid> some other text
another line

Edit

And applied to the example from your comment

'<MessageID>9348399343-38493-Det-100</MessageID>' -replace '(?s)(?<=messageid>).+?(?=</messageid)',(Get-Date).ToString('dddd dd-MM-yyyy HH:mm:ss')

<MessageID>Monday 12-10-2020 21:58:56</MessageID>
Sign up to request clarification or add additional context in comments.

Comments

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.