title: PowerShell: Character Animation in Console and SetCursorPosition got crazy
body: <p>Experts, 
  I'm building a long list of Visual Studio projects using msbuild. And while hiding the msbuild output, displaying a status and an animation based upon some characters is displayed while msbuild doing its job. Here my script to build using msbuild and display wait animation (You will surely like this...):</p>
  
  <pre><code>$anim=@("|","/","-","\","|") # Animation sequence characters
  $ReturnCode = @{}
  $BuildStatus="Building $(ProjectName)" 
  
  $CursorTop=[Console]::CursorTop #// Cursor position on Y axis
  $CursorLeft=$BuildStatus.Length #// Cursor position on X axis 
  Write-Host $BuildStatus -ForegroundColor Cyan
  
  #// starting the MsBuild job in background.
  
  $MsbJob = [PowerShell]::Create().AddScript(
  {
    param($MsbArgs, $Result)
    &amp; msbuild $MsbArgs | Out-Null
    $Result.Value = $LASTEXITCODE
  }
  ).AddArgument($MsbArgs).AddArgument($ReturnCode)
  
  $async = $MsbJob.BeginInvoke() #// start executing the job.
  
  #// While above script block is doing its job in background, display status and animation in console window.
  
  while (!$async.IsCompleted)
  {
      foreach ($item in $anim) #// looping on array containing characters to form animation
      {
  
         [Console]::SetCursorPosition($CursorLeft + 5,$CursorTop) #//setting position for cursor
         Write-Host $item -ForegroundColor Yellow 
         Start-Sleep -m 50
      }
  }
  
  $MsbJob.EndInvoke($async)
  </code></pre>
  
  <p>most of the time, it works as expected i.e. showing the status like:</p>
  
  <blockquote>
    <p><strong>Building MyProject (Animating characters)..</strong></p>
  </blockquote>
  
  <p>but suddenly, it becomes like this:<img src="https://i.stack.imgur.com/ofHsQ.png" alt="enter image description here"></p>
  
  <p><strong>Only Discovered Solution:</strong> I was able to correct this is to increase the Screen Buffer Size of Console (Console Properties --> Layout: Screen Buffer Size) to 1024. In addition, I also increased Window Size to fill out the monitor and it didn't break like that.</p>
  
  <p>Is my assessment about Screen Buffer is correct? or anything else is breaking up?
  If Yes, do I have to increase the screen buffer size pragmatically i.e. from inside my .psm1 script.?</p>
  
  <p>Any help would be really appreciated.</p>