title: Empty variable after Invoke-Command
body: <p>I want to get the most errors of a list of servers and save it in the variable <code>$AllErrors</code>.</p>
  
  <p>But the variable is empty if I want to print it out.</p>
  
  <p>Is there a way to pass the variable out of the <code>Invoke-Command</code>?</p>
  
  <p>This is my code:</p>
  
  <pre><code>Get-Content -Path C:\Users\$env:USERNAME\Desktop\Server.txt |
      ForEach-Object{
          Invoke-Command -ComputerName $_ -ScriptBlock{
  
              $Date = (Get-Date).AddHours(-12)
              $Events = Get-WinEvent -FilterHashtable @{LogName = 'System'; StartTime = $Date; Level = 2}
              $Errors = $Events | Group-Object -Property ID -NoElement | Sort-Object -Property Count -Descending |
                  Select-Object -Property Name -First 1
  
          }
      }
  $Errors | Out-File -FilePath C:\Users\$env:USERNAME\Desktop\AllErrors.txt
  </code></pre>