Function/Tool/Write-MyProgress.ps1
function Write-MyProgress () { <# .SYNOPSIS Displays a progress bar within a Windows PowerShell command window. .DESCRIPTION The Write-Progress cmdlet displays a progress bar in a Windows PowerShell command window that depicts the status of a running command or script. .NOTES File Name : Write-MyProgress.ps1 Author : Thomas ILLIET, contact@thomas-illiet.fr Date : 2017-05-10 Last Update : 2018-01-08 Version : 1.0.1 .PARAMETER id Specifies an ID that distinguishes each progress bar from the others. .PARAMETER ParentId Specifies the parent activity of the current activity. .PARAMETER StartTime StartTime of the foreach processing .PARAMETER Object Object use in your foreach processing .PARAMETER Count Foreach Count status .EXAMPLE $GetProcess = Get-Process $Count = 0 $StartTime = Get-Date foreach($Process in $GetProcess) { $Count++ Write-MyProgress -StartTime $StartTime -Object $GetProcess -Count $Count write-host "-> $($Process.ProcessName)" Start-Sleep -Seconds 1 } #> Param( [parameter(Mandatory=$true)] [Array]$Object, [parameter(Mandatory=$true)] [DateTime]$StartTime, [parameter(Mandatory=$true)] [Int]$Count, [parameter(Mandatory=$false)] [Int]$Id=1, [parameter(Mandatory=$false)] [Int]$ParentId=-1 ) $SecondsElapsed = ((Get-Date) - $StartTime).TotalSeconds $SecondsRemaining = ($SecondsElapsed / ($Count / $Object.Count)) - $SecondsElapsed $Argument = @{ Activity = "Processing Record $Count of $($Object.Count)" PercentComplete = (($Count/$($Object.Count)) * 100) CurrentOperation = "$("{0:N2}" -f ((($Count/$($Object.Count)) * 100),2))% Complete" SecondsRemaining = $SecondsRemaining } if($Id -ne $null) { $Argument += @{ Id = $Id } } if($ParentId -ne $null) { $Argument += @{ ParentId = $ParentId } } Write-Progress @Argument } |