get SID for all users in AD

Anne 281 Reputation points
2022-02-23T00:54:07.45+00:00

I would like to pull all our users SID from Active directory.
I was able to pull distinguishedname,samaccountname,givenname etc, but I cannot pull SID from user property.
it returns blank.
function ADQuery($filter, $props)
{
$domain = New-Object System.DirectoryServices.DirectoryEntry
$search = New-Object System.DirectoryServices.DirectorySearcher
$search.SearchRoot = $domain
$search.PageSize = 10000
$search.Filter = $filter
$search.SearchScope = "Subtree"
return $search.FindAll()
}

function GetADUsers()
{
$filter = '(&(objectCategory=user)(sAMAccountName=)(sAMAccountType=)(employeeID=H*))'

$props = @("sn", "givenname",  "samaccountname", "userprincipalname","sid" ,"email","employeeid", "distinguishedname", "title" )

$results = AdQuery $filter $props    
$outRowsRows  = @()
foreach ($result in $results)
{
    $item = $result.Properties;          
    $dn = $item.distinguishedname
    $employeeid = $item.employeeid
    $username = $item.samaccountname
    $username = $username -replace ",", " "
    $sid=$item.objectSid
    write-host "this is the sid:$sid"
     write-host $item.SID

....

could anyone help? thanks

Windows for business | Windows Server | User experience | PowerShell
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Limitless Technology 44,751 Reputation points
    2022-02-25T15:38:41.127+00:00

    Hello @Anne

    You can extract all the SIDs in a specific domain using:

    Get-ADUser -Filter * -SearchBase "dc=domain,dc=local" | select Name,SID

    Hope this helps with your query,

    --
    --If the reply is helpful, please Upvote and Accept as answer--

    2 people found this answer helpful.
    0 comments No comments

  2. Rich Matheisen 47,901 Reputation points
    2022-02-25T21:13:32.347+00:00

    The property name, I think, should be objectSID.

    Also, why are you using "ADquery" instead of the PowerShell Get-ADUser?

    1 person found this answer helpful.
    0 comments No comments

  3. Anne 281 Reputation points
    2022-02-25T23:37:34.843+00:00

    I got an easier one:
    (Get-ADUser myusername).SID.value


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.