DirectorySearcher and custom AD attributes

David Zemdegs 1,541 Reputation points
2021-03-25T22:58:49.797+00:00

Greetings,
I am using the directorysearcher to retrieve the value of a custom AD attribute.
I am at a loss to explain this behaviour when I try to retrieve it.
I first run this code:

$searcher = New-Object DirectoryServices.DirectorySearcher
$searcher.Filter = "(&(name=Computer1)(objectClass=computer))"
$searcher.pageSize = 1000
$ADSearchResults = $searcher.FindAll()

I then try to display the custom attribute by running this command:

$ADSearchResults.Properties.CustomReboot

...and nothing is displayed.
If I type this:

$ADSearchResults.Properties.CustomReboot[0]

...then the value is returned - however if I type this:

$ADSearchResults.Properties

...all the properties are returned - now here's the thing - if I now type this:

$ADSearchResults.Properties.CustomReboot

It returns a value where it didnt when I first typed it after the findall command.
Why is this happening?

Thanks
David Z

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,320 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Rich Matheisen 44,416 Reputation points
    2021-03-26T15:19:21.2+00:00

    Add "$searcher.PropertiesToLoad = 'CustomReboot'".

    PowerShell makes life easy, but if you're dealing with .Net you have to do the work. ;-)


  2. David Zemdegs 1,541 Reputation points
    2021-03-28T23:46:08.32+00:00

    Now the behaviour is getting weirder.
    I tried retrieving the value for computer A and
    $ADSearchResults.Properties.CustomReboot[0] didnt work where
    $ADSearchResults.Properties.CustomReboot did.

    but for computer B
    $ADSearchResults.Properties.CustomReboot didnt work where
    $ADSearchResults.Properties.CustomReboot[0] did.

    0 comments No comments