Select-Object -expandproperty ... a time saver!!!!
Have you ever run a powershell command and used select-object to filter the returned object? If you have you will know that even if you only have one value in the select, you still have to refer to the property name to return the values. Example :
I want a list of all my enabled DC's
But to get to the first hostname I have to write this :
Have you ever run a powershell command and used select-object to filter the returned object? If you have you will know that even if you only have one value in the select, you still have to refer to the property name to return the values. Example :
I want a list of all my enabled DC's
PS C:\temp> $dcs = get-ADDomainController -filter {enabled -eq $True} | select HostName
PS C:\temp> $dcs
HostName
--------
DC01.DOMAIN.COM
DC02.DOMAIN.COM
DC03.DOMAIN.COM
DC04.DOMAIN.COM
But to get to the first hostname I have to write this :
$dcs[0].hostnameIf I were to use -expandproperty as below :
PS C:\temp> $dcs = get-ADDomainController -filter {enabled -eq $True} | Select-Object -ExpandProperty hostnameI now have an array of server names that I can simply push through a foreach, without the '.hostname'!
PS C:\temp> $dcs
DC01.DOMAIN.COM
DC02.DOMAIN.COM
DC03.DOMAIN.COM
DC04.DOMAIN.COM
Comments
Post a Comment