Skip to content

Instantly share code, notes, and snippets.

@hoetz
Last active September 6, 2018 07:30
Show Gist options
  • Save hoetz/9ae40a2e82bef982397d69b23f250b34 to your computer and use it in GitHub Desktop.
Save hoetz/9ae40a2e82bef982397d69b23f250b34 to your computer and use it in GitHub Desktop.
Find users from foreign domains in a Active Directory group and output them to CSV
#Find users from foreign domains in a Active Directory group and output them to CSV
$group=get-adgroup "mygroup" -Properties member
$domainController="mydc.company.loc"
$fsps = $group.member | where {$_ -like "*foreign*"}
$outputUsers = @()
foreach ($fsp in $fsps) {
$found = $fsp -match 'S-\d-\d-\d+-\d+-\d+-\d+-\w+'
if ($found) {
$usr = get-aduser $matches[0] -server $domainController -Properties displayname, useraccountcontrol, description, mail | select -Property samaccountname, displayname, useraccountcontrol, description, mail
$outputUsers += $usr
}
}
$outputUsers | export-csv -Path "c:\temp\fsp-general.csv"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment