List Attributes in a Variable for Use With Get-ADComputer

Hi! I have a short script that prompts the user for an asset number, and then pulls two attributes from AD for that computer object. If the asset number is left blank, it pulls the two attributes for all computers. Initially, the script included this command:

Get-ADComputer -Filter “Name -like ‘*$AssetNo’” -Properties * | Format-Table Name, BillingClient, VLAN -AutoSize

BillingClient and VLAN are custom attributes that were added to AD. Also, I am using -SearchBase, but I removed it here for brevity.

I found that using “-Properties BillingClient, VLAN” is way faster. But say that I wanted to add 5 more attributes. I could add each one in the command, but then the command could be extremely long. Is there a way to include all the needed attributes in a variable, and then use the variable in the Get-ADComputer command?

Thanks for any help that you can offer!

–Tom

Hi,

You can specify the properties as an array:

$properties = @(
    BillingClient
    VLAN
    otherProperty1
    otherProperty2
    otherProperty3
)

Get-ADComputer -Filter "Name -like '*$AssetNo'" -Properties $properties | Format-Table -Property $properties -AutoSize

Before commands get long, you should always consider splatting:

Have a read, then see if you can re-write the above command using that technique.

By the way, when posting code or data, please be sure to format your post using the </> button (it may be hidden under the gear icon). This makes it easier to read and also makes it easier for us to copy and paste your code for testing.

How to format code on PowerShell.org

Thanks! I knew there was a way to do that, but I couldn’t remember or find it! I will tuck that away for future reference, and I’ll take a look at splatting.

Thanks again!

–Tom