How can I trim properties between pipelines in a one-liner?

Welcome Forums General PowerShell Q&A How can I trim properties between pipelines in a one-liner?

Viewing 3 reply threads
  • Author
    Posts
    • #34704
      Participant
      Topics: 7
      Replies: 16
      Points: 0
      Rank: Member

      Hi,
      I want to write this command-line (one-liner) to get members of an ad-Group and place them i a textfile. I have so far written the following:

      Get-ADGroupMember xyzGroup | select samaccountname | out-file c:\temp\group.txt

      But how do i trim the samaccountname. In the final text file there are spaces after each name on each line.

      I can easily do this in a script:

      $Members = Get-ADGroupMember xyzGroup
      Foreach ($obj in $Members)
      {
      $navn = $obj.samaccountname.Trim()
      $navn | out-file “c:\temp\xyzGroup_members.txt” -Append
      }

      But how do i do the same thing in the one-liner?

    • #34708
      Participant
      Topics: 10
      Replies: 122
      Points: 2
      Rank: Member

      somthing like this I think

      # to store in Variable
      $Members = Get-ADGroupMember xyzGroup -Properties Samaccountname | %{$_.Samaccountname.Trim()} 
      
      # to send straight to file
      Get-ADGroupMember xyzGroup -Properties Samaccountname | %{$_.Samaccountname.Trim()} | out-file "c:\temp\xyzGroup_members.txt" -Append
      
    • #34713
      Participant
      Topics: 16
      Replies: 108
      Points: 0
      Rank: Member

      Michael,

      You can also try

      (Get-ADGroupMember -Identity YourGroupName).SamAccountName | out-file c:\test\xyzGroup_members.txt -Append
      
    • #34721
      Participant
      Topics: 7
      Replies: 16
      Points: 0
      Rank: Member

      Thanks for the help guys, you solved my problem 🙂

Viewing 3 reply threads
  • The topic ‘How can I trim properties between pipelines in a one-liner?’ is closed to new replies.