Replace UID in a string

Hey guys,

I’m new to powershell and any of it’s machinations thus I would like to get some help with a -replace issue which I’m stuck with.

I’m trying to replace string in logs due to data security reason. What needs to be replaced is:

LogOnUsers : Companyname\UserID

I would like to replace the actual UserID with “UserID” to mask the identity of the users.

The userid always starts with a country code 2 char followed 6 char unique identifier so it is 8 char overall. I need to find this line in a large description string and replace it starting after companyname\

What I’ve tried is:

-replace “companyname\.+?” , “UserID” // which is not good as this will only replace the line starting from the companyname\userID with UserID and I’m left with whatever is longer then the UserID so 6 char. Can you help with a regex solution which takes it into consideration the UserID is not a fixed value but it is constant 8 char at least what might help.


Thanks in advance!


Just try below option.
-replace ‘\[^\s]*’,’\UserID’

It is under the assumption that your line contains “company\userName” followed by a space. The regex will look for a backslash in a string and it will replace the text after the backslash with UserID. If you have a backslash in any other place, then try below

-replace ‘companyname\[^\s]*’,’\UserID’