1

when i do a net group (the name of the group) /domain in powershell I get a list like this. Multiple items in a row with many white spaces in between:

groupmemberOne           groupmemberTwo           groupmemberThree
groupmemberFour          groupmemberFive          groupmemberSix

I want to extract/transform the output of net group and send it to a txt file via a script in the following format:

groupmemberOne
groupmemberTwo  
groupmemberThree
groupmemberFour
groupmemberFive     
groupmemberSix

what i did so far:

New-Item -Path "thepath and .txt file" -ItemType File -Force
$data = net group /domain (the name of the group)
$transform = $data[8..$($data.Count -3)]
$result = $transform | ConvertFrom-String | out-string
Add-Content -Value $transform -Path "thepath and .txt file"

Can you help me?

mklement0
  • 382,024
  • 64
  • 607
  • 775
  • 1
    I think it will be better to use native PowerShell cmdlets instead of CMD tools. Have you tried using [Get-LocalGroupMember](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.localaccounts/get-localgroupmember?view=powershell-5.1) – Olaf Nov 08 '21 at 20:55
  • 1
    @Olaf, a PowerShell-native solution is definitely better, but note that the question is about _domain_ groups. – mklement0 Nov 08 '21 at 22:16

1 Answers1

3

Note:

  • Generally, it's better to use PowerShell commands, so you don't have to rely on parsing text and can operate objects and their properties: see Get-ADUserGroupMember; in the simplest case (doesn't distinguish between members of type user, computer, and group):

    (Get-ADUserGroupMember $groupName).Name > $outFile
    
  • If you don't have the ActiveDirectory module installed / cannot install it, you can use the [adsi] class (short for: System.DirectoryServices.DirectoryEntry), as Santiago Squarzon recommends:


If you want to stick with your text-parsing approach, use the unary form of the -split operator to parse the output from your net.exe group call, which splits strings by runs of whitespace, ignoring leading and trailing whitespace:

# ...
-split $transform > $outFile

Caveat: This won't work if the group member names themselves contains space. If they do, you'd have to split by runs of multiple spaces, under the assumption that such runs are exclusive to the whitespace separating the names:

# ... 
$transform -split '  +' > $outFile # split by 2 or more spaces

Note: > is an effective alias of Out-File, which means that in Windows PowerShell you'll get a "Unicode"-encoded file (UTF-16LE). If that is undesired, pipe to | Set-Content -Encoding utf8 $outFile, for instance.

To illustrate with simple examples that simulate the relevant part of net group's output:

PS> -split (
     'groupmemberOne           groupmemberTwo           groupmemberThree',
     'groupmemberFour          groupmemberFive          groupmemberSix'
    )

groupmemberOne
groupmemberTwo
groupmemberThree
groupmemberFour
groupmemberFive
groupmemberSix
PS> (
     'groupmember One           groupmember Two           groupmember Three',
     'groupmember Four          groupmember Five          groupmember Six'
    ) -split '  +'

groupmember One
groupmember Two
groupmember Three
groupmember Four
groupmember Five
groupmember Six
mklement0
  • 382,024
  • 64
  • 607
  • 775