Replace UID in a string

Welcome Forums General PowerShell Q&A Replace UID in a string

Viewing 1 reply thread
  • Author
    Posts
    • #188269
      Participant
      Topics: 1
      Replies: 0
      Points: 12
      Rank: Member

      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!

       

    • #188317
      Participant
      Topics: 0
      Replies: 26
      Points: 124
      Helping Hand
      Rank: Participant

      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'
      
Viewing 1 reply thread
  • You must be logged in to reply to this topic.