Skip to content

Instantly share code, notes, and snippets.

@yoav-lavi
Created April 30, 2018 08:38
Show Gist options
  • Select an option

  • Save yoav-lavi/1253321d968db7f52d1a77ac48e3ff96 to your computer and use it in GitHub Desktop.

Select an option

Save yoav-lavi/1253321d968db7f52d1a77ac48e3ff96 to your computer and use it in GitHub Desktop.
A spinner for PowerShell
function Load {
param([scriptblock]$function,
[string]$Label)
$job = Start-Job -ScriptBlock $function
$symbols = @("⣾⣿", "⣽⣿", "⣻⣿", "⢿⣿", "⡿⣿", "⣟⣿", "⣯⣿", "⣷⣿",
"⣿⣾", "⣿⣽", "⣿⣻", "⣿⢿", "⣿⡿", "⣿⣟", "⣿⣯", "⣿⣷")
$i = 0;
while ($job.State -eq "Running") {
$symbol = $symbols[$i]
Write-Host -NoNewLine "`r$symbol $Label" -ForegroundColor Green
Start-Sleep -Milliseconds 100
$i++
if ($i -eq $symbols.Count){
$i = 0;
}
}
Write-Host -NoNewLine "`r"
}
@regg00
Copy link

regg00 commented Aug 16, 2023

Thanks for this mate.
I'm gonna use it in my project here https://github.com/regg00/ChocoMan/tree/main

@eabase
Copy link

eabase commented Feb 3, 2026

Very nice!
Run like this:

Load -function { pip list --outdated } -Label "Checking packages..."

Q: But you don't see the output!?
A: Run Receive-Job -Job <job#>

An improvement is to add the following lines at the end of the function:

# ...

# To get the result output from the call:
# Use "-Keep" to keep job data after 1st Receive-Job read.
$RES = Receive-Job -Job $job
Remove-Job -Job $job

Return $RES

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment