PowerShell for Windows Admins

Feb 22 2012   1:13PM GMT

object vs value



Posted by: Richard Siddaway
PowerShell

An interesting question came up recently. A Powershell user was trying to access an AD attribute so they did something like this

$x = Get-ADUser -Identity usera -Properties * | select useraccountcontrol

 

When they tried to use $x it didn’t correctly in the rest of the script.

 

Select-object is used to filter down the attributes that are left on the object as it passes on the pipeline

So

PS> Get-ADUser -Identity usera -Properties * | select useraccountcontrol

useraccountcontrol
        ——————
                        512

 

If you just want the value rather than an object (I know that its still an object but in reality we work directly with the value) then use –expandproperty. On a property with a single value it returns that value

PS> Get-ADUser -Identity usera -Properties * | select -expandproperty useraccountcontrol
512

Comment on this Post

Leave a comment: