This example shows two PowerShell scripts. The first one shows the possibilities for the write-progress CmdLet. The second PowerShell script is a proper example script, showing all printers. For demo purposes, some start-sleep CmdLet’s are added. 


The first script has OutputSettings default 1023, all write info is shown immediately in the textbox and provided back to the result. The second example has the normal output written back to the result. But all other output is not shown.


Note that the ‘start-sleep’ is internally replaced by ‘start-wait’, which handles forms refresh better that the original ‘start-sleep’. 

The example script is easy to read  for the average PowerShell user. So just take a look at the code. There is also enough information and examples on the internet for PowerScript. 



'First run a a demo powershell script to show the effect of the write-progress cmdlet

'Note that the start-sleep cmdlet is internally replaced by 'start-wait'.

'This is an internal sleeper, because the original start-sleep does not allow refresh of

'the main screen


'Remove the remark if you want to skip the first part

'goto SKIP


[RUNPS]

## A powerscript to show the functionality of the Write-Progress CmdLet

clear-host


Write "Line 1 test"

Write-Progress -Activity "Run Powerscript" -Status "Set progressbar" -PercentComplete 25

start-sleep -s 2


Write "Line 2 Test"

Write-Progress -Activity "Run Powerscript" -Status "More Info" -PercentComplete 50 -CurrentOperation "Operation Text" -SecondsRemaining 6

start-sleep  -s 2


Write "Line 3 Test"

Write-Progress -Activity "Run Powerscript" -Status "Other Info" -PercentComplete 75 -CurrentOperation "Operation Text"

start-sleep  -s 2


Write "Line 4 Test"

Write-Progress -Activity "Run Powerscript" -Status "Other Info" -PercentComplete 100 -SecondsRemaining 3700

start-sleep  -s 2


Write "Line 5 Test"

Write-Progress -Activity "Run Powerscript" -Status "Progress completed!" -Completed

start-sleep  -s 2


clear-host

[/RUNPS]


'Just to label, so that the first part can be skipped with ease.

LABEL: SKIPFIRSTPART


'only write commands are shown in the output, no write events

SetOutputSetting(1)



'Now a real Powerscript

[RUNPS]

## WMI query to list all properties and values of the Win32_Printer class

## This PowerShell script was generated using the WMI Code Generator, Version 1.30

## http://www.robvanderwoude.com/updates/wmigen.html


param( [string]$strComputer = "." )


$colItems = get-wmiobject -class "Win32_Printer" -namespace "root\CIMV2" -computername $strComputer

$counter = 0


foreach ($objItem in $colItems) {

       write-host "Name                           :" $objItem.Name

       write-host "Default                        :" $objItem.Default

       write-host "Network                        :" $objItem.Network

       write-host "Port Name                      :" $objItem.PortName

       write-host "Driver Name                    :" $objItem.DriverName

       write-host "Server Name                    :" $objItem.ServerName

       write-host "Share Name                     :" $objItem.ShareName

       write-host 

       

       ## use counter and calculation for progressbar

       $a = [int](($counter/$colItems.count)*100)

       Write-Progress -Activity "Get Printers" -Status $objItem.Name -PercentComplete $a

       start-sleep  -m 500

       $counter = $counter +1

}

## Just to demo, set the status to 100%

Write-Progress -Activity "Get Printers" -Status "List completed!" -PercentComplete 100

start-sleep  -m 500


## Set the status as completed

Write-Progress -Activity "Get Printers" -Status "List completed!" -Completed

[/RUNPS]


'wait for a while to see that the textoutput is not updated

sleep(1000)


'Now set the result of the latest powershell script to the output

output = [#PSRESULT#]

Created with the Personal Edition of HelpNDoc: Free help authoring tool