Powershell script to gather users in a domain group and export to a csv

ullmanneric
ullmanneric used Ask the Experts™
on
I am trying to create a powershell script that will pull a list of groups from a text file and then output the users in those groups to a csv file for each group.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
You can use the quest tools or AD cmdlets


$Groups = Get-content c:\groups.txt
$Groups | Foreach {
Get-qadgroupmember $_ | export-csv "C:\$($_.)csv"
}



$Groups = Get-content c:\groups.txt
$Groups | Foreach {
Get-adgroupmember $_ | export-csv "C:\$($_.)csv"
}

Author

Commented:
That one does not work exactly what I want since I only want users names in a particular group so in theory I want the report to list the group name and then the users in that group and that is it. The groups names should come from a text file

Commented:
sorry forgott o add something


$Groups = Get-content c:\groups.txt
$Groups | Foreach {
Get-qadgroupmember $_ | Select samaccountname | export-csv "C:\$($_.)csv"
}
Starting with Angular 5

Learn the essential features and functions of the popular JavaScript framework for building mobile, desktop and web applications.

Author

Commented:
It doesnt work when I put two groups in the groups.txt file the last group to ruin overrides the first group in the csv file so I dont get the info I need

Author

Commented:
SO I need the account name and Full name fields also I need each group to be lested right now I am running two groups to test test1 and test2 users in test2 are overiding the csv file so basically the last group is overriding everything I need all the groups listed whether they are seperate tabs or something

Commented:
the csv files should be the name of the groups in the text file. So if you have group1 and group2 you should have two file group1.csv and group2.csv

for the full names do this,


$Groups = Get-content c:\groups.txt
$Groups | Foreach {
Get-qadgroupmember $_ | Select name, samaccountname | export-csv -Delimiter ; "C:\$($_.)csv"
}

Author

Commented:
yeah that is not working that way

Author

Commented:
It is basically giving an error

Author

Commented:
Missing property name after reference operator.
At line:2 char:4
+ $_. <<<<
    + CategoryInfo          : ParserError: (.:String) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : MissingPropertyName

[PS] H:\>
Commented:
was typing to fast, had the "." in the wrong spot.


$Groups = Get-content c:\groups.txt
$Groups | Foreach {
Get-qadgroupmember $_ | Select name, samaccountname | export-csv -Delimiter ; "C:\$($_).csv"
}

Author

Commented:
still no go

Export-Csv : Missing an argument for parameter 'Delimiter'. Specify a parameter of type 'System.Char' and try again.
At line:2 char:76
+ Get-qadgroupmember $_ | Select name, samaccountname | export-csv -Delimiter <<<<  ; "C:\$($_).csv"
    + CategoryInfo          : InvalidArgument: (:) [Export-Csv], ParameterBindingException
    + FullyQualifiedErrorId : MissingArgument,Microsoft.PowerShell.Commands.ExportCsvCommand

C:\g-lan infrastructure.csv
Export-Csv : Missing an argument for parameter 'Delimiter'. Specify a parameter of type 'System.Char' and try again.
At line:2 char:76
+ Get-qadgroupmember $_ | Select name, samaccountname | export-csv -Delimiter <<<<  ; "C:\$($_).csv"
    + CategoryInfo          : InvalidArgument: (:) [Export-Csv], ParameterBindingException
    + FullyQualifiedErrorId : MissingArgument,Microsoft.PowerShell.Commands.ExportCsvCommand

C:\g-group.csv

Commented:
put quotes around the ;

so like this

-Delimiter ";"

Author

Commented:
SO I am now getting the user name but is there a way to get the Full name as well which will make it easier for my management as I really want the username and the Full name

Commented:
Using "Select name, samaccountname " should give you that.

Can you try this and see what is returned


get-qaduser YOURUSERNAME | Select name, samaccountname

and if that does not work try this to give you all the default attributes to see what you want.

get-qaduser YOURUSERNAME | Select *

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial