Trim() issue (trying to trim a string)

Welcome Forums General PowerShell Q&A Trim() issue (trying to trim a string)

Viewing 3 reply threads
  • Author
    Posts
    • #250634
      Participant
      Topics: 2
      Replies: 2
      Points: 27
      Rank: Member

      I have a powershell script whose output looks like below:

       

      UserListFormat { SchemaVersion: 2, Mode: Open, Users: [(“[email protected]”: UserEntry { Mode: Whitelist, FriendlyName:
      “domain\John” }), (“[email protected]”: UserEntry { Mode: Whitelist, FriendlyName: “domain\Jack” }), (“_Mike.K”:
      UserEntry { Mode: Whitelist, FriendlyName: “domain\Mike” })] }

       

      I just need FriendlyName and its corresponding value as a list in the output and get rid of everything else. Below is the output I want to achieve.

      FriendlyName: “domain\John”

      FriendlyName: “domain\Jack”

      FriendlyName: “domain\Mike”

       

      I tried using trim() but its hard to achieve the desired output. I also tried select-string which doesn’t work.

      Can someone help me achieve this?

       

    • #250673
      Participant
      Topics: 2
      Replies: 25
      Points: 92
      Helping Hand
      Rank: Member

      Hi,

      Have you tried:

      $Output = “Your PS output with domain names”

      ([regex]’\w*\\\w*’).Matches($Output) | ForEach-Object {$_.Value}`

      It does assume your names don(t contain numbers and stuff. If so you’ll have to add those characters to the regex

      Kris.

      • This reply was modified 1 month, 1 week ago by Kris.
    • #250700
      Participant
      Topics: 8
      Replies: 568
      Points: 2,170
      Helping Hand
      Rank: Community Hero

      You can use

      First create a template. You have to remove extra curly braces in the template.

      Now let’s run it

      Output

      It’s also captured in the $names variable and you can access the contents like this

      And this is how you’d do the same thing reading from a file

    • #250853
      Participant
      Topics: 0
      Replies: 81
      Points: 362
      Helping Hand
      Rank: Contributor

      If your output is truly a single string with no line breaks or newline characters, you can do the following:

      ($YourString | Select-String -Pattern 'FriendlyName:([^"]*?"){2}' -AllMatches).Matches.Value

Viewing 3 reply threads
  • You must be logged in to reply to this topic.