Spinner2.ps1
function Show-Spinner { param ( [scriptblock]$ScriptBlock ) process { # Don't show the cursor $Host.UI.RawUI.CursorSize = 0 $spinner = @("-", "\", "|", "/", "-", "\", "|", "/") $i = 0 $finished = $false # Create a PowerShell instance and run the scriptblock in the current runspace $psInstance = [powershell]::Create().AddScript($ScriptBlock).AddScript({ # Capture all variables in the scriptblock $ScriptBlockVariables = Get-Variable | ForEach-Object { [PSCustomObject]@{ Name = $_.Name; Value = $_.Value } } # Return the captured variables return $ScriptBlockVariables }) $jobHandle = $psInstance.BeginInvoke() while (-not $finished) { if ($i -ne 0) { Write-Host "`b" -NoNewline } Write-Host $spinner[$i % $spinner.Length] -NoNewline $i = ($i + 1) Start-Sleep -Milliseconds 70 $finished = $jobHandle.IsCompleted } $cursorPos = $Host.UI.RawUI.CursorPosition $cursorPos.Y -= 1 $Host.UI.RawUI.CursorPosition = $cursorPos } end { # Retrieve the result from the scriptblock $variables = $psInstance.EndInvoke($jobHandle) Write-Host "" # Set the variables in the parent scope foreach ($variable in $variables) { Set-Variable -Name $variable.Name -Value $variable.Value -Scope 1 } } } $abe = 1 $MyStuff = Show-Spinner -ScriptBlock { Start-Sleep -Seconds 3 $var1 = 1..10 $var2 = $false $var3 = "Hello" $var4 = "World" } Write-Host "HEJ :D . The result is $($var1), $($var2), $($var3), $($var4)" |