title: PowerShell - iterating over computer names in Active Directory
body: <p>I'm new to PowerShell and I'm attempting to write a script that will query AD for machine names, check which ones are responding and write the output into a file. So far I have this:</p>
  
  <pre><code>$computers = Get-ADComputer -filter {(Name -like "PC*")} | Select-Object -Property Name
  foreach ($computer in $computers) {
      if((Test-Connection -Cn $computer -BufferSize 16 -Count 1 -Ea 0 -Quiet)) {
          "Machine $computer connected." | Out-File "out.txt" -Append
      } else {
          "Machine $computer not connected." | Out-File "out.txt" -Append
      } #end if
  } #end foreach
  </code></pre>
  
  <p>What I get in the output text file looks like the following:</p>
  
  <pre><code>...
  Machine @{Name=PC-0649} not connected.
  Machine @{Name=PC-1541} not connected.
  Machine @{Name=PC-1574} not connected.
  ...
  </code></pre>
  
  <p>I think my problem lies with the <code>Select-Object -Property Name</code> part of the first line. Running the debugger, it looks like PowerShell is formatting each iteration of <code>$computer</code> to include the header line.</p>
  
  <pre><code>[DBG]: PS Y:\&gt;&gt; $computer
  
  Name                                                                                                                                                                                          
  ----                                                                                                                                                                                          
  PC-0649
  </code></pre>
  
  <p>What's the best way for me to strip out everything except the PC-#### part in this situation?</p>