Skip to content

Instantly share code, notes, and snippets.

@nordineb
Created March 27, 2018 06:23
Show Gist options
  • Save nordineb/18c08ef430a42a9cf65a888c7985bff2 to your computer and use it in GitHub Desktop.
Save nordineb/18c08ef430a42a9cf65a888c7985bff2 to your computer and use it in GitHub Desktop.
List all SPNs used in your Active Directory
cls
$search = New-Object DirectoryServices.DirectorySearcher([ADSI]"")
$search.filter = "(servicePrincipalName=*)"
## You can use this to filter for OU's:
## $results = $search.Findall() | ?{ $_.path -like '*OU=whatever,DC=whatever,DC=whatever*' }
$results = $search.Findall()
foreach( $result in $results ) {
$userEntry = $result.GetDirectoryEntry()
Write-host "Object Name = " $userEntry.name -backgroundcolor "yellow" -foregroundcolor "black"
Write-host "DN = " $userEntry.distinguishedName
Write-host "Object Cat. = " $userEntry.objectCategory
Write-host "servicePrincipalNames"
$i=1
foreach( $SPN in $userEntry.servicePrincipalName ) {
Write-host "SPN(" $i ") = " $SPN
$i+=1
}
Write-host ""
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment