0
votes

I'm trying to retain the Microsoft.ActiveDirectory.Management.ADAccount object when searching and specifying return outputs; however, when selecting the outputs, additional fields are populating. Questions:

  1. Why is this is happening?
  2. Is there a method, command, or filter to return only the specified parameters? (not the extra fields listed in the example below)
  3. Is there a way to remove properties from the ADAccount Object? ($a in this example)
  4. For the select method, is there a way to retain the original object formatting ? (I do not want a table and still need to reference the object later)

Running the following command:

$a = Get-ADUser $targetPerson -Properties Department, EmailAddress, Office, OfficePhone

returns:

Department        : ****
DistinguishedName : CN=1111,OU=2222,OU=3333,OU=4444,DC=5555,DC=6666
EmailAddress      : ****@mail.com
Enabled           : ****
GivenName         : ****
Name              : ****
ObjectClass       : user
ObjectGUID        : ****
Office            : ****
OfficePhone       : ****
SamAccountName    : ****
SID               : ****
Surname           : ****
UserPrincipalName : ****
2

2 Answers

0
votes

Get-ADUser has a default set of properties it always returns, including for instance the distinguished name, the SID, and the account name. The parameter -Properties is for specifying which additional properties the cmdlet should return, because the default property set is just a small subset of all available properties.

To limit the output of Get-ADUser to a specific set of properties you need to pipe the output through Select-Object:

$props = 'Department', 'EmailAddress', 'Office', 'OfficePhone'
$a = Get-ADUser $targetPerson -Properties $props |
     Select-Object $props

Of course that will turn the ADAccount object into a custom object (PSCustomObject), but I don't think there's a way around that.

0
votes

The object Microsoft.ActiveDirectory.Management.ADAccount inherits members from the class it is created from. Here are some of the inherited members - Name, ObjectClass, ObjectGUID, SID, and SamAccountName.

More about AD Object

I don't think you can create a Microsoft.ActiveDirectory.Management.ADAccount object with out those inherited members.

But if your project can accept a PSCustomObject then pipe $a to Select-object.

$a | Select-Object -Property Department, EmailAddress, Office, OfficePhone