0

I have a command that loads a filtered list of servers into a variable:

$Computers = Get-adcomputer -filter "OperatingSystem -notlike '*server*'" -properties * |Where-Object {$_.LastLogonDate -gt $time -and ($_.Enabled -eq $true) -and ($_.name -ne 'Rose*')} |select name

And I want to use a For each loop to do stuff with those computernames. When I just put the command in I get a list with "name" at the top and a list of computernames

But when I use a Write out the contents of $Computer in loop I get a list of @{name=Computer1}

What is going on? Why can't I get a clean array of computers to work with?

Squirreljester
  • 191
  • 1
  • 1
  • 7

1 Answers1

2

For every input object piped to select Name, it's going to create a new object that has a single property Name, with the value copied from the corresponding property on the input object.

To get only the value of the Name property on the input objects, use Select-Object's -ExpandProperty parameter:

$Computers = Get-adcomputer -filter "OperatingSystem -notlike '*server*'" -properties * |Where-Object {$_.LastLogonDate -gt $time -and ($_.Enabled -eq $true) -and ($_.name -ne 'Rose*')} |Select -ExpandProperty Name

Now, $computers will be an array of strings, instead of an array of objects with a Name property

Mathias R. Jessen
  • 157,619
  • 12
  • 148
  • 206