To provide status updates, use the
Write-Progress cmdlet shown in Example 13-5.##############################################################################
##
## Invoke-LongRunningOperation
##
## From Windows PowerShell Cookbook (O'Reilly)
## by Lee Holmes (http://www.leeholmes.com/guide)
##
##############################################################################
<#
.SYNOPSIS
Demonstrates the functionality of the Write-Progress cmdlet
#>
Set-StrictMode -Version Latest
$activity = "A long running operation"
$status = "Initializing"
## Initialize the long-running operation
for($counter = 0; $counter -lt 100; $counter++)
{
$currentOperation = "Initializing item $counter"
Write-Progress $activity $status -PercentComplete $counter `
-CurrentOperation $currentOperation
Start-Sleep -m 20
}
$status = "Running"
## Initialize the long-running operation
for($counter = 0; $counter -lt 100; $counter++)
{
$currentOperation = "Running task $counter"
Write-Progress $activity $status -PercentComplete $counter `
-CurrentOperation $currentOperation
Start-Sleep -m 20
}The
Write-Progress cmdlet provides a way for you to provide structured status information to the users of your script for long-running operations.
Like the other detailed information channels (
Write-Debug, Write-Verbose, and the other Write-* cmdlets), PowerShell lets users control how much of this information they see.For more information about the
Write-Progress cmdlet, type Get-Help Write-Progress.
Learn more about this topic from Windows PowerShell Cookbook, 2nd Edition.
This introduction to the Windows PowerShell language and scripting environment provides more than 430 task-oriented recipes to help you solve the most complex and pressing problems, and includes more than 100 tried-and-tested scripts that intermediate to advanced system administrators can copy and use immediately. You'll find hands-on tutorials on fundamentals, common tasks, and administrative jobs that you can apply whether you're on a client or server version of Windows.

Help


