PowerShell v2 Simple Write Progress Example

I like the idea of write progress, but, it can be a little confusing depending on how complex your wiring up of the counters is. Using one of the Get-Help examples I wrote this to display a progress bar while iterating through a GCI collection:
$begin = {$i=0;$out=}
$end = { $out }
$process = {
      $i++;
      Write-Progress-PercentComplete ($i/$filecount*100) -Activity Showing simple counter-Status Progress:
}
$files = Get-ChildItemC:Windowssystem32
$filecount = $files.count
$files |
ForEach-Object -Begin $begin -Process $process -end $end
This is really nothing more than window dressing as it does nothing more than show this (where more os are added as it iterates over the collection:
Showing simple counter
   Progress:
   [ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo                                 ]
In reality, I used this to indicate how long a file copy was running which looked like this:
# Collect file info
$filecount = ($files = Get-ChildItem -Path $BaseFolder -Recurse).count
$begin = {$i=0;$out=}
$end = { $out }
$process = {
      $i++;
      Write-Progress -PercentComplete ($i/$filecount*100) -Activity Copying files -Status Progress:
      Write-Verbose "$(Write-TimeStamp): Copying $($_.fullname).";
      Copy-Item -Path $_.fullname -Destination ($_.fullname -replace test,$environment)                           
}
Also, a nice trick I try to use whenever I can is the first line. Gather two pieces of information in one line:
 $filecount = ($files = Get-ChildItem -Path $BaseFolder -Recurse).count

Related Posts by Categories

0 comments:

Post a Comment