Reputation: 31
I am trying to create list of computers which I can work on using Get-Computer. Here are two ways to create the list. I don’t understand why they are different or how I can get the results of the two methods to have the same value.
My code is:
$ComputerList1 = "WS7-HUR-MANAGER","WS7-HUR-NURSE"
$ComputerList2 = Get-ADComputer -Filter {Name -like "WS7-HUR*"} | Select` Name
Get-Variable ComputerList1 | Select Name, Value
Get-Variable ComputerList2 | Select Name, Value
The output:
Name Value
---- -----
ComputerList1 {WS7-HUR-MANAGER, WS7-HUR-NURSE}
ComputerList2 {@{Name=WS7-HUR-MANAGER}, @{Name=WS7-HUR-NURSE}}
Upvotes: 3
Views: 59
Reputation: 23355
$ComputerList1
contains contains a string array object.
$ComputerList2
contains a custom PowerShell object that was returned by Get-ADComputer and then filtered to only include it's Name
property.
You can see the difference between the two by piping each to Get-Member.
$ComputerList1 | Get-Member
$ComputerList2 | Get-Member
Per the other answer you can use the -ExpandProperty
switch of the Select-Object
cmdlet to take one property of the object and return its contents as a new object of it's type. E.g if the property contained a string, you would then get a string object. E.g:
$ComputerList2 = Get-ADComputer -Filter {Name -like "WS7-HUR*"} | Select ExpandProperty Name
Or you can also do this to access the property directly and assign it to a variable:
$ComputerList2 = (Get-ADComputer -Filter {Name -like "WS7-HUR*"}).Name
Upvotes: 3
Reputation: 1346
$ComputerList2 = Get-ADComputer -Filter {Name -like "WS7-HUR*"} | Select -ExpandProperty Name
Upvotes: 1