title: Show output on screen and in file in PowerShell
body: <p>How can I get all this to not only output on the screen, but to save to a text file in <a href="http://en.wikipedia.org/wiki/Comma-separated_values" rel="noreferrer">CSV</a> format?</p>
  
  <pre><code>$OUs = Get-ADObject -LDAPFilter "(objectCategory=organizationalUnit)" `
       -SearchBase "OU=GA,OU=EAST,DC=corp,DC=chm,DC=com" | Select distinguishedName
   ForEach ($OU In $OUs)
   {
       $OU.distinguishedName
       Get-ADComputer -SearchBase $OU.distinguishedName -SearchScope OneLevel `
           -Filter * | Select Name
   }
  </code></pre>
  
  <p>I have tried</p>
  
  <pre><code>$OUs = Get-ADObject -LDAPFilter "(objectCategory=organizationalUnit)" `
       -SearchBase "OU=GA,OU=EAST,DC=corp,DC=chartercom,DC=com" | Select distinguishedName
   ForEach ($OU In $OUs)
   {
       $OU.distinguishedName
       Get-ADComputer -SearchBase $OU.distinguishedName -SearchScope OneLevel `
           -Filter * | Select Name
   } | | export-CSV c:\temp\outfile.csv –noType
  </code></pre>
  
  <p>And many other formats, but I always get the error:</p>
  
  <blockquote>
    <p>An empty pipe element is not allowed.</p>
  </blockquote>