Created
August 17, 2018 12:13
-
-
Save seriald/c5100b33395c6a1183e58f123b7dff5e to your computer and use it in GitHub Desktop.
Export a list of users within an AD Group given an OU
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$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