Author Posts

September 10, 2014 at 11:20 pm

$allcomputers = $AllServers = Get-ADComputer -Filter * -Property * -SearchBase 'OU=a,OU=b,OU=c,DC=domain,DC=local'

$computers = $Allcomputers | select -Property Name

$computer not equal to name is show me as arry

this is what i get

Name
—-
OHAD_B

how can i get the name i want to pass the $computers to foreach loop

foreach ($computer in $computers.Name) {

if (test-Connection -Cn $computer -quiet) {

}
}

Plaes Help

TNX
ITamar

September 20, 2014 at 9:59 pm

Use "Select -expandproperty name" instead of "Select -property name".

$names = Get-ADComputer -filter * -searchbase "ou=a,dcdomain,dc=local" | select -expand name
foreach ($name in $names) {

}

September 20, 2014 at 10:14 pm

Tnx,
I see your powershell in lunch
Why the different ?
what the different between -expandproperty and -property ?

sincerity
Itamar

September 21, 2014 at 7:49 am

The difference is that with -ExpandProperty you get an array of strings that represent names whereas if you just use -Property you will get an array of custom objects that contain a name column.

Example:

$names = Get-ADComputer -filter * -searchbase "ou=a,dcdomain,dc=local" | select -expand name
foreach ($name in $names) {
    Test-Connection $name
}

If you use just -Property name you would do:

$names = Get-ADComputer -filter * -searchbase "ou=a,dcdomain,dc=local" | select -property name
foreach ($name in $names) {
    Test-Connection $name.name
}