How can I retrieve a specific property exclusively?
I am aware of the select-object
cmdlet which seems to be dowdy in that respect:
PS C:\> Get-ADOrganizationalUnit -SearchBase 'OU=Houston,DC=contoso,DC=net' -Filter 'Name -like "SomeOU"' -Properties * | Select-Object Description,Streetaddress,State,postalcode | format-list
An optimized version would be:
PS C:\> Get-ADOrganizationalUnit -SearchBase 'OU=Houston,DC=contoso,DC=net' -Filter 'Name -like "SomeOU"' -Properties Description,Streetaddress,State,postalcode
Why is the -property
switch not solely returning the entered propteries?
This is because the
-properties
switch is not a formatting tool, it is intended as a way to receive more information than the default values already included.From the Get-ADOrganizationalUnit article.
If you want to format your output, you're better of sticking with the
select-object
statement.You would need to use both, to specify which properties to retrieve from the DC, and which ones to select and ultimately display.
-Properties *
is a potential performance basher since the DSA will need to return every attribute that has a value, including certificates and other binary values you might not have need forIn a script utilizing the AD cmdlets I would use the splatting operator (
@
) and do the following:If you do this consistently, your scripts become über-easy to update/edit, since every query is defined in the same readable hastable format
If I'm at the shell and mid-oneliner come to think about the properties I need, I'd do something like (using
Get-ADUser
as an example):Also consider using LDAP or ADSI http://blogs.technet.com/b/heyscriptingguy/archive/2010/12/01/use-powershell-to-query-active-directory-from-the-console.aspx