\n Hi, and welcome to the PowerShell forum! \n\n\nDon’t apologize for being a “noob” or “newbie” or “n00b.” There’s just no need – nobody will think you’re stupid, and the forums are all about asking questions. Just ask!\nUse a descriptive subject. Don't say \"Need help\" or \"PowerShell Help\", actually summarize what the problem is. It helps the rest of us keep track of which problem is which.\nDon’t post massive scripts. We’re all volunteers and we don’t have time to read all that, nor will we copy, past…\n <\/blockquote>\n<\/aside>\n\n <\/p>","upvoteCount":0,"datePublished":"2019-06-26T17:58:25.000Z","url":"https://community.spiceworks.com/t/ps-help-pull-users-from-specific-group-and-ou/718343/2","author":{"@type":"Person","name":"Neally","url":"https://community.spiceworks.com/u/Neally"}},{"@type":"Answer","text":"
Why do you have a filter in there? I assume you want to get the info for each student?<\/p>\n
maybe like this?<\/p>\n
$OUs = \"OU=Sites,DC=bored,DC=test,DC=org\", \"OU=Sites,DC=bored,DC=test,DC=org\"\nForEach ($OU in $OUs) {\n Get-ADGroupMember -identity \"Students\" | \n Get-ADUser -SearchBase $OU -Property DisplayName, EmailAddress | \n Export-Csv \"C:\\Users\\blah\\Desktop\\Student-OL.csv\" -Append\n}\n<\/code><\/pre>","upvoteCount":0,"datePublished":"2019-06-26T18:06:22.000Z","url":"https://community.spiceworks.com/t/ps-help-pull-users-from-specific-group-and-ou/718343/3","author":{"@type":"Person","name":"Neally","url":"https://community.spiceworks.com/u/Neally"}},{"@type":"Answer","text":"Correct about getting the info for each student.<\/p>\n
Thank you for your assistance.<\/p>\n
I get this when I run the provided PS:<\/p>\n
cmdlet Get-ADUser at command pipeline position 2 \nSupply values for the following parameters: \n(Type !? for Help.) \nFilter:<\/p>","upvoteCount":0,"datePublished":"2019-06-26T18:10:00.000Z","url":"https://community.spiceworks.com/t/ps-help-pull-users-from-specific-group-and-ou/718343/4","author":{"@type":"Person","name":"alexdiaz4","url":"https://community.spiceworks.com/u/alexdiaz4"}},{"@type":"Answer","text":"\n\n
<\/div>\n
drftordie:<\/div>\n
\nCorrect about getting the info for each student.<\/p>\n
Thank you for your assistance.<\/p>\n
I get this when I run the provided PS:<\/p>\n
cmdlet Get-ADUser at command pipeline position 2 \nSupply values for the following parameters: \n(Type !? for Help.) \nFilter:<\/p>\n<\/blockquote>\n<\/aside>\n
Do you need the searchbase? Can you filter for the OU before or after?<\/p>","upvoteCount":0,"datePublished":"2019-06-26T18:13:14.000Z","url":"https://community.spiceworks.com/t/ps-help-pull-users-from-specific-group-and-ou/718343/5","author":{"@type":"Person","name":"Neally","url":"https://community.spiceworks.com/u/Neally"}},{"@type":"Answer","text":"
\nDo you need the searchbase? Can you filter for the OU before or after?<\/p>\n<\/blockquote>\n
I am fairly new to PS.<\/p>\n
I do need tot he searchbase so that it can also separate by OU. I do not see why before or after would not work. How would I go about doing this?<\/p>","upvoteCount":0,"datePublished":"2019-06-26T18:18:31.000Z","url":"https://community.spiceworks.com/t/ps-help-pull-users-from-specific-group-and-ou/718343/6","author":{"@type":"Person","name":"alexdiaz4","url":"https://community.spiceworks.com/u/alexdiaz4"}},{"@type":"Answer","text":"
If you want to get users from a group, you shouldn’t need the OU.<\/p>\n
Get-ADGroupMember -identity \"Students\" | ForEach { Get-ADUser -Property DisplayName, EmailAddress } | Export-Csv \"C:\\Users\\blah\\Desktop\\Student-OL.csv\" -Append\n<\/code><\/pre>","upvoteCount":1,"datePublished":"2019-06-26T18:19:16.000Z","url":"https://community.spiceworks.com/t/ps-help-pull-users-from-specific-group-and-ou/718343/7","author":{"@type":"Person","name":"Evan7191","url":"https://community.spiceworks.com/u/Evan7191"}},{"@type":"Answer","text":"\n\n
<\/div>\n
Evan7191:<\/div>\n
\nIf you want to get users from a group, you shouldn’t need the OU.<\/p>\n
Get-ADGroupMember -identity \"Students\" | ForEach { Get-ADUser -Property DisplayName, EmailAddress } | Export-Csv \"C:\\Users\\blah\\Desktop\\Student-OL.csv\" -Append\n<\/code><\/pre>\n<\/blockquote>\n<\/aside>\nEvan,<\/p>\n
The issue is that some staff are in that group as well. With that I need to also filter by OU, so that only the students in the student OU will show up.<\/p>\n
Thanks<\/p>","upvoteCount":0,"datePublished":"2019-06-26T18:21:07.000Z","url":"https://community.spiceworks.com/t/ps-help-pull-users-from-specific-group-and-ou/718343/9","author":{"@type":"Person","name":"alexdiaz4","url":"https://community.spiceworks.com/u/alexdiaz4"}},{"@type":"Answer","text":"\n\n
<\/div>\n
Neally:<\/div>\n
\nI’d do it like this:<\/p>\n
$OUs = \"OU=Sites,DC=bored,DC=test,DC=org\", \"OU=Sites,DC=bored,DC=test,DC=org\"\n$result = \nForEach ($OU in $OUs) {\n get-aduser -filter * -SearchBase $ou -Properties DisplayName, EmailAddress, memberof |\n where memberof -match 'Students'\n}\n\n$result | Export-Csv \"C:\\Users\\blah\\Desktop\\Student-OL.csv\"\n<\/code><\/pre>\nOhhh ok I get what you did. That makes more sense. This worked great. \nThanks for your assistance.<\/p>\n<\/blockquote>\n<\/aside>","upvoteCount":0,"datePublished":"2019-06-26T18:31:44.000Z","url":"https://community.spiceworks.com/t/ps-help-pull-users-from-specific-group-and-ou/718343/10","author":{"@type":"Person","name":"alexdiaz4","url":"https://community.spiceworks.com/u/alexdiaz4"}},{"@type":"Answer","text":"\n\n
<\/div>\n
drftordie:<\/div>\n
\n\n\n
<\/div>\n
Evan7191:<\/div>\n
\nIf you want to get users from a group, you shouldn’t need the OU.<\/p>\n
Get-ADGroupMember -identity \"Students\" | ForEach { Get-ADUser -Property DisplayName, EmailAddress } | Export-Csv \"C:\\Users\\blah\\Desktop\\Student-OL.csv\" -Append\n<\/code><\/pre>\n<\/blockquote>\n<\/aside>\nEvan,<\/p>\n
The issue is that some staff are in that group as well. With that I need to also filter by OU, so that only the students in the student OU will show up.<\/p>\n
Thanks<\/p>\n<\/blockquote>\n<\/aside>\n
In that case, you have two options:<\/p>\n
\n\nQuery users based on OU, as Neally suggested.<\/p>\n<\/li>\n
\nQuery users based on the group and then filter based on a property.<\/p>\n<\/li>\n<\/ol>\n
Get-ADGroupMember -identity \"Students\" | ForEach { Get-ADUser -Property DisplayName, EmailAddress } | Where-Object {$_.distinguishedName -match \"OU1\" -or $_.distinguishedName -match \"OU2\"} | Export-Csv \"C:\\Users\\blah\\Desktop\\Student-OL.csv\" -Append\n<\/code><\/pre>","upvoteCount":2,"datePublished":"2019-06-26T18:32:17.000Z","url":"https://community.spiceworks.com/t/ps-help-pull-users-from-specific-group-and-ou/718343/11","author":{"@type":"Person","name":"Evan7191","url":"https://community.spiceworks.com/u/Evan7191"}},{"@type":"Answer","text":"You can also just use the filter directly:<\/p>\n
$OUs = \"OU=Sites,DC=bored,DC=test,DC=org\", \"OU=Sites,DC=bored,DC=test,DC=org\"\n$Results = Foreach ($OU in $OUs) {\n Get-ADUser -Filter \"memberof -match 'CN=Group,DC=bored,DC=test,DC=org'\" -SearchBase $OU\n } \n$Results | Export-Csv \"C:\\Users\\blah\\Desktop\\Student-OL.csv\"\n<\/code><\/pre>\nSome additional options (recursive, not in the group)<\/p>\n
$OUs = \"OU=Sites,DC=bored,DC=test,DC=org\", \"OU=Sites,DC=bored,DC=test,DC=org\"\n$Results = Foreach ($OU in $OUs) {\n Get-ADUser -Filter \"-not memberof -RecursiveMatch 'CN=Group,DC=bored,DC=test,DC=org'\" -SearchBase $OU\n } \n$Results | Export-Csv \"C:\\Users\\blah\\Desktop\\Student-OL.csv\"\n<\/code><\/pre>","upvoteCount":1,"datePublished":"2019-06-27T07:34:41.000Z","url":"https://community.spiceworks.com/t/ps-help-pull-users-from-specific-group-and-ou/718343/12","author":{"@type":"Person","name":"davelewis56","url":"https://community.spiceworks.com/u/davelewis56"}}]}}
Neally
(Neally)
June 26, 2019, 5:58pm
2
If you post code, please use the ‘Insert Code’ button. Please and thank you!
Hi, and welcome to the PowerShell forum!
Don’t apologize for being a “noob” or “newbie” or “n00b.” There’s just no need – nobody will think you’re stupid, and the forums are all about asking questions. Just ask!
Use a descriptive subject. Don't say "Need help" or "PowerShell Help", actually summarize what the problem is. It helps the rest of us keep track of which problem is which.
Don’t post massive scripts. We’re all volunteers and we don’t have time to read all that, nor will we copy, past…