How can I correct my foreach loop in powershell for every Active Directory OU user?

Goal: Create a for or foreach loop to execute some code (in this case, just print x) for every user in an OU. Im using powershell 2.0, with ActiveDirectory module.

So far: This is what I have (see below). It just prints out the X's for every user. But its not working how I want it to, instead I think it may be doing it for every line. So I get 6 X's for 'name', '----', 'test1', 'test2', SPACE, SPACE.

$pool = Get-ADUser -Filter * -SearchScope Subtree -SearchBase "OU=Test,OU=Users,OU=jack,DC=Corp,DC=jill,DC=com" -Properties name | FT name
foreach ($user in $pool )
{ write-host "x"}
$pool

Result, the SPACE will be represented by a period (.) :

x
x
x
x
x
x


name                        
----                      
test1                  
test2
.
.

I am not sure why it does this. If you have a better method or way of handling this, I'll be delighted in hearing it.


Your $pool will contain the output of Format-Table name, the last step in the first line. The Format-* cmdlets are used for displaying values on screen. You almost certainly don't want to feed a formatted table to your foreach loop.

$pool = Get-ADUser -Filter * -SearchScope Subtree -SearchBase "OU=Test,OU=Users,OU=jack,DC=Corp,DC=jill,DC=com"
foreach ($user in $pool) {
  Write-Host "x"
}

# And if you really want to see an `ft $pool`:
$pool | Format-Table name