Skip to content

Instantly share code, notes, and snippets.

@seriald
Created August 17, 2018 12:13
Show Gist options
  • Save seriald/c5100b33395c6a1183e58f123b7dff5e to your computer and use it in GitHub Desktop.
Save seriald/c5100b33395c6a1183e58f123b7dff5e to your computer and use it in GitHub Desktop.
Export a list of users within an AD Group given an OU
$OU = Read-Host -Prompt 'Enter the OU' #Example: OU=IMB,OU=HQ,DC=intra,DC=pri
$Group = Read-Host -Prompt 'Enter the AD Group' #Example: XA_Restricted_Visio
$Users = Get-ADUser -filter * -SearchBase $OU | Select samaccountname
$Members = Get-ADGroupMember -Identity $Group | Select samaccountname
Clear-Content -Path "F:\Data\Scripts\Export\*"
ForEach ($User in $Users)
{
$Name = $User.samaccountname
If ($Members -Match $User.samaccountname)
{
Write-Output $Name >> "F:\Data\Scripts\Export\Export_OU_$Group.txt"
Write-Host "$Name written to file" }
Else
{
Write-Host "$Name does not exist in $Group"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment