Last active
December 9, 2022 16:17
-
-
Save dancing-groot/b6b381c48e98409b70cb84c810893b2e to your computer and use it in GitHub Desktop.
Generic function for using progress bars - based on https://adamtheautomator.com/write-progress/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#region FUNCTIONS | |
function Write-ProgressHelper | |
{ | |
<# | |
.SYNOPSIS | |
Generic function for using progress bars | |
.LINK | |
https://gist.github.com/dancing-groot/b6b381c48e98409b70cb84c810893b2e | |
.NOTES | |
Version: 2022.12.09 | |
Author: @dancing-groot | |
#> | |
[CmdletBinding(DefaultParameterSetName = 'Running')] | |
param ( | |
[Parameter(Mandatory = $true, ParameterSetName = 'Running')][string]$Message, | |
[Parameter(Mandatory = $true, ParameterSetName = 'Running')][int]$StepNumber, | |
[Parameter(ParameterSetName = 'Running')][int]$TotalSteps = $script:ProgressTotalSteps, | |
[Parameter(ParameterSetName = 'Running')][Parameter(ParameterSetName = 'Finished')][string]$Activity = $script:ProgressActivity, | |
[Parameter(Mandatory = $true, ParameterSetName = 'Finished')][switch]$Completed | |
) | |
switch ($PSCmdlet.ParameterSetName) | |
{ | |
'Running' { Write-Progress -Activity $Activity -Status $Message -PercentComplete (($StepNumber / $TotalSteps) * 100) } | |
'Finished' { Write-Progress -Activity $Activity -Completed } | |
} | |
} # Write-ProgressHelper | |
#endregion FUNCTIONS | |
#region EXAMPLE1 | |
$Folder = "C:\Program Files\Common Files" | |
$Foo = Get-ChildItem -Path $Folder -Directory | |
# Declare variables for example 1 progress bar | |
$script:ProgressTotalSteps = $Foo.count | |
$script:ProgressActivity = "List Folders in '$Folder'" | |
$StepCounter = 1 | |
# Run example 1 | |
Write-Host "EXAMPLE 1 - If you only need one progress bar in your script" -ForegroundColor Green | |
Write-ProgressHelper -Message "Starting..." -StepNumber $StepCounter | |
foreach ($Bar in $Foo) | |
{ | |
Write-Host $Bar.Name | |
Write-ProgressHelper -Message $Bar.Name -StepNumber ($StepCounter++) | |
Start-Sleep -Seconds 1 | |
} | |
Write-ProgressHelper -Completed | |
Write-Host "EXAMPLE 1 COMPLETED." | |
Start-Sleep 1 | |
#endregion EXAMPLE1 | |
#region EXAMPLE2 | |
# Declare variables for example 2 progress bar | |
$CustomProgressTotalSteps = 10 | |
$CustomProgressActivity = "Counting from 1 to $CustomProgressTotalSteps" | |
$CustomArgs = @{ | |
Activity = $CustomProgressActivity | |
TotalSteps = $CustomProgressTotalSteps | |
} | |
# Run example 2 | |
Write-Host "EXAMPLE 2 - If you need to run another progress bar in your script" -ForegroundColor Green | |
for ($i = 1; $i -le $CustomProgressTotalSteps; $i++) | |
{ | |
Write-Host $i | |
Write-ProgressHelper @CustomArgs -Message "$i/$CustomProgressTotalSteps" -StepNumber $i | |
Start-Sleep 1 | |
} | |
Write-ProgressHelper -Activity $CustomProgressActivity -Completed | |
Start-Sleep 1 | |
Write-Host "EXAMPLE 2 COMPLETED." | |
#endregion EXAMPLE2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment