Avatar of ullmanneric
ullmanneric
 asked on

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

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.
Powershell

Avatar of undefined
Last Comment
KenMcF

8/22/2022 - Mon
KenMcF

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"
}
ullmanneric

ASKER
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
KenMcF

sorry forgott o add something


$Groups = Get-content c:\groups.txt
$Groups | Foreach {
Get-qadgroupmember $_ | Select samaccountname | export-csv "C:\$($_.)csv"
}
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
ullmanneric

ASKER
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
ullmanneric

ASKER
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
KenMcF

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"
}
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
ullmanneric

ASKER
yeah that is not working that way
ullmanneric

ASKER
It is basically giving an error
ullmanneric

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

[PS] H:\>
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
ASKER CERTIFIED SOLUTION
KenMcF

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
ullmanneric

ASKER
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
KenMcF

put quotes around the ;

so like this

-Delimiter ";"
ullmanneric

ASKER
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
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
KenMcF

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 *