AdComputer

$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

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) {

}

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

sincerity
Itamar

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
}