title: PowerShell: Measure-Command outputs nice Response Time list to console; when outputted to text file its one very long string of text
body: <p>I've found that the PowerShell Measure-Command outputs a nice Response Time list to console, but when outputted to text file it becomes one very long string of text.  This is a problem because it becomes hard to read when viewed with an HTML reader.  I've struggled with this one for quite a bit this evening, so now posting this to SO.</p>
  <pre><code>import-module activedirectory
  cls
  $myForest = [System.DirectoryServices.ActiveDirectory.Forest]::GetCurrentForest()
  $domaincontrollers = $myforest.Sites | % { $_.Servers } | Select Name
  $report = foreach ($DomainController in $DomainControllers) 
  {
    $totalmeasurement = 0
    $i = 0
    while ($i -ne $numberoftests)
    {
    $measurement = (Measure-Command {Get-ADUser Administrator -Server $DomainController.name}).TotalSeconds
    $totalmeasurement += $measurement
    $i += 1
    }
    $totalmeasurement = $totalmeasurement / $numberoftests
    &quot;Domain Controller: &quot; + $DomainController.name + &quot;, Response time: &quot; + $totalmeasurement + &quot; seconds&quot;
  }
  </code></pre>
  <p>The above code when run inside a vanilla environment will produce output looking like this first screenshot:
  <a href="https://i.stack.imgur.com/5XE3g.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/5XE3g.png" alt="Pretty Result" /></a></p>
  <hr />
  <p>With a slight modification to the code (as shown below) by setting <strong>foreach</strong> to a variable:</p>
  <pre><code>import-module activedirectory
  cls
  $myForest = [System.DirectoryServices.ActiveDirectory.Forest]::GetCurrentForest()
  $domaincontrollers = $myforest.Sites | % { $_.Servers } | Select Name
  $report = foreach ($DomainController in $DomainControllers) 
  {
    $totalmeasurement = 0
    $i = 0
    while ($i -ne $numberoftests)
    {
    $measurement = (Measure-Command {Get-ADUser Administrator -Server $DomainController.name}).TotalSeconds
    $totalmeasurement += $measurement
    $i += 1
    }
    $totalmeasurement = $totalmeasurement / $numberoftests
    &quot;Domain Controller: &quot; + $DomainController.name + &quot;, Response time: &quot; + $totalmeasurement + &quot; seconds&quot;
  }
  $report | Out-String | Out-File $htmlfile_temp -Append  
  </code></pre>
  <p>The file output winds up looking the mess shown in this second screenshot:</p>
  <p><a href="https://i.stack.imgur.com/ND1qW.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ND1qW.png" alt="Messy Result" /></a></p>
  <p>I've tried many different permutations of the original code to output to the file formatted nicely as shown in the first screenshot, to no avail.  I've given up and am posting the question to this forum.  I am well aware of the limitations of write-host, and is one of many reasons I am outputting to an HTML file instead.</p>
  <p>(Original code from this post found <a href="https://social.technet.microsoft.com/wiki/contents/articles/26227.active-directory-measure-dc-response-time-with-powershell-when-running-ldap-query.aspx" rel="nofollow noreferrer">here</a>)</p>