Skip to content

Instantly share code, notes, and snippets.

Last active Oct 23, 2017
What would you like to do?
$ForeignUsers = Get-DomainObject -Properties objectsid,distinguishedname -SearchBase "GC://" -LDAPFilter '(objectclass=foreignSecurityPrincipal)' | ? {$_.objectsid -match '^S-1-5-.*-[1-9]\d{2,}$'} | Select-Object -ExpandProperty distinguishedname
$Domains = @{}
$ForeignMemberships = ForEach($ForeignUser in $ForeignUsers) {
# extract the domain the foreign user was added to
$ForeignUserDomain = $ForeignUser.SubString($ForeignUser.IndexOf('DC=')) -replace 'DC=','' -replace ',','.'
# check if we've already enumerated this domain
if (-not $Domains[$ForeignUserDomain]) {
$Domains[$ForeignUserDomain] = $True
# enumerate all domain local groups from the given domain that have any membership set
Get-DomainGroup -Domain $ForeignUserDomain -Scope DomainLocal -LDAPFilter '(member=*)' -Properties distinguishedname,member | ForEach-Object {
# check if there are any overlaps between the domain local groups and the foreign users
if ($($_.member | Where-Object {$ForeignUsers -contains $_})) {
$ForeignMemberships | fl
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment