Skip to content

Instantly share code, notes, and snippets.

@h4sh5
Last active December 7, 2024 23:42
Show Gist options
  • Save h4sh5/a215681c62b7dea22cf30ae2b565be81 to your computer and use it in GitHub Desktop.
Save h4sh5/a215681c62b7dea22cf30ae2b565be81 to your computer and use it in GitHub Desktop.
Powershell query AD LDAP without RSAT

(New-Object DirectoryServices.DirectorySearcher("(&(objectCategory=group)(name=Domain Admins))")) .FindAll().Properties['Member']

Working better with output: (function from https://petri.com/expanding-active-directory-searcher-powershell/)

Function Convert-ADSearchResult {
[cmdletbinding()]
Param(
[Parameter(Position = 0,Mandatory,ValueFromPipeline)]
[ValidateNotNullorEmpty()]
[System.DirectoryServices.SearchResult]$SearchResult
)
Begin {
    Write-Verbose "Starting $($MyInvocation.MyCommand)"
}
Process {
    Write-Verbose "Processing result for $($searchResult.Path)"
    #create an ordered hashtable with property names alphabetized
    $props = $SearchResult.Properties.PropertyNames | Sort-Object
    $objHash = [ordered]@{}
    foreach ($p in $props) {
     $value =  $searchresult.Properties.item($p)
     if ($value.count -eq 1) {
        $value = $value[0]
     }
     $objHash.add($p,$value)
    }
new-object psobject -property $objHash
}
End {
    Write-Verbose "Ending $($MyInvocation.MyCommand)"
}
}

^ paste that in the console to use the function

E.g. do a description audit

(New-Object DirectoryServices.DirectorySearcher("(&(objectCategory=user)(|(description=*pass*)(comment=*pass*)(note=*pass*)))")).FindAll() | Convert-ADSearchResult | Select adspath, description, comment, note, when* | out-gridview

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment