1

I'm trying to end up with a txt file of usernames (1 user per line) using the

net group /domain <group>

command. However, the output of this command is not conducive to parsing. Does anyone have a way to pipe this to a txt file so that the file consists of only one user per line?

kylex
  • 1,371
  • 5
  • 13
  • 18
  • 4
    No Powershell? `Get-ADGroupMember "groupname" | Select name | Export-Csv -NoTypeInformation output.csv` – jscott Jul 30 '14 at 16:54
  • @jscott, when I run that command in PS, I get: "The term Get-ADGroupMember is not recognized as the name of a cmdlet...". Also I'd prefer to use a command that doesn't require additional installations if at all possible. – kylex Jul 30 '14 at 16:55
  • 1
    You need Microsoft's [Active Directory Powershell module](http://technet.microsoft.com/en-us/library/ee617195.aspx). It's included in [RSAT](http://www.microsoft.com/en-us/download/details.aspx?id=7887) and Server 2008/2012. – jscott Jul 30 '14 at 16:57
  • Might use [`dsget group`](http://technet.microsoft.com/en-us/library/cc731202.aspx) instead of `net /group`. Powershell with the AD modules is the right way though. – Zoredache Jul 30 '14 at 18:03

1 Answers1

4

Ok, no additional installs, I get it. The following is horrible, horrible, and I would suggest there must be a better way [use the AD module!] to do it. I didn't feel like writing this in batch just now, so here it is. Note there isn't even a hint of i18n, error handling or anything "sane" here.

# Powershell
$result = net group /domain "SomeGroup"
($result |
    Where-Object { $_ -notmatch '^(The request|$|Group Name|Comment|Members|--|The c)' }).Split(' ') |
        Where-Object { $_ } |
             Out-File group.txt
jscott
  • 24,204
  • 8
  • 77
  • 99