Tuesday, March 19, 2019

Progress bar in Power-Shell


Progress bar function is very important function in power shell. It’s always good to use show progress bar when you are running some process in your script. GUI effect makes you script much interactive and informative .Here  I am discussing  some of the power shell progress bar .We my be help full to someone.




#################################################################################

for($I = 1; $I -lt 101; $I++ )
{
    Write-Progress -Activity Updating -Status 'Progress->' -PercentComplete $I -CurrentOperation OuterLoop
    for($j = 1; $j -lt 101; $j++ )
    {
        Write-Progress -Id 1 -Activity Updating -Status 'Coping the file' -PercentComplete $j -CurrentOperation InnerLoop
    }
}

#################################################################################




################################# Function on progress bar ###########################

function Progress-Bar($iSeconds,$sInUnString)
{
    $Host.UI.RawUI.BackgroundColor = ($bckgrnd ='Black')                                                               # Changing the progress bar color and background text color
   # $Host.PrivateData.ProgressForegroundColor ='Green'
   # $Host.PrivateData.ProgressBackgroundColor='Black'

    $DoneDT = (Get-Date).AddSeconds($iSeconds)
    while($DoneDT -gt (Get-Date))
{
        $iSecondsLeft = $DoneDT.Subtract((Get-Date)).TotalSeconds
        $iPercent = ($iSeconds - $iSecondsLeft) / $iSeconds * 100
        Write-Progress -Activity "$sInUnString Apple.." -Status " " -PercentComplete $iPercent
        [System.Threading.Thread]::Sleep(500)
    }
 }

 Progress-Bar "20" "Best Company to work"

#################################################################################



############################### Time you can give here ##############################

$totalTimes = 10

  $i = 0

  for ($i=0;$i -lt $totalTimes; $i++) {

  $percentComplete = ($i / $totalTimes) * 100

  Write-Progress -Activity 'Work in Progress' -Status "Did thing $i  times" -PercentComplete $percentComplete

  sleep 1

}

#################################################################################




################################# Pause Program for 15 min #########################

###===========================
### Pause Program for 15 min
### - Matt Brown, 2008
###===========================
$x = 15*60
$length = $x / 100
while($x -gt 0) {
  $min = [int](([string]($x/60)).split('.')[0])
  $text = " " + $min + " minutes " + ($x % 60) + " seconds left"
  Write-Progress "Pausing Script" -status $text -perc ($x/$length)
  start-sleep -s 1
  $x--
}

#################################################################################




################################## Three progress bar################################

for ($i = 1; $i -le 2; $i++)
{
    Write-Progress -ID 1 -Activity "Outer loop" -Status "Tick $i" -percentComplete ($i / 2*100)
    for ($j = 1; $j -le 3; $j++)

    {
               Write-Progress -ID 2 -Activity "Mid loop" -Status "Tick $j" -percentComplete ($j / 3*100)
               for ($k = 1; $k -le 3; $k++)

           {

            Write-Progress -ID 3 -Activity "Inner loop" -Status "Tick $k" -percentComplete ($k / 3*100)
            Sleep(1)
           }
    }

}

#################################################################################

No comments:

Post a Comment