Use the blue Nav bar above to access the main index pages!

List SPNs in Active Directory using PowerShell

How to list SPNs in Active Directory using PowerShell. A Service Principal Name (SPN) is a service name that is registered in Active Directory, and is associated with a computer or user account (the security context in which the service runs).  The standard AD tools don't give you a good way to figure out where an SPN is registered, or list what SPNs are registered in your AD.

To locate a specific SPN, check out our posts Finding an SPN in Active Directory using Perl and Finding an SPN in Active Directory using VBScript.

The PowerShell script below finds all SPNs in your domain of the service type that you specify.  In the example below, I have the service type set to "HTTP", so the script returns all of the HTTP/ SPNs.  The script uses the directorySearcher .Net class to find the accounts with HTTP/ SPNs.

$spns = @{}
$filter = "(servicePrincipalName=$serviceType/*)"
$domain = New-Object System.DirectoryServices.DirectoryEntry
$searcher = New-Object System.DirectoryServices.DirectorySearcher
$searcher.SearchRoot = $domain
$searcher.PageSize = 1000
$searcher.Filter = $filter
$results = $searcher.FindAll()
foreach ($result in $results){
 $account = $result.GetDirectoryEntry()
 foreach ($spn in $account.servicePrincipalName.Value){
$spns.keys ¦ sort-object


Sameer said...

This works with no issues. Thanks

Anonymous said...

This is great, thank you. Works better than some more recent offerings I've found, since it finds user accounts (not just computer accounts) that have SPNs bound to them.

I just tweaked the DirectorySearcher to the newer syntax.

$search = New-Object DirectoryServices.DirectorySearcher([ADSI]"")
$search.PageSize = 1000
$search.filter = "(servicePrincipalName=*)"
$results = $search.Findall()

robyn farley said...

AMAZING! Thank you very much, this saved me untold hours.

Post a Comment

Related Posts Plugin for WordPress, Blogger...