You sit, patiently waiting. Staring hopefully at a console screen while the cursor blinks repeatedly. You typed in the command or executed your script as you’ve done countless times before, and while it’s worked in the past, this time the screen simply lingers there while the command processes in the background, and you’re still wondering if it’s working. If only there was a way to know for sure (hint: There is).
SEE: Incident response policy (TechRepublic Premium)
How many times has this scenario happened to all of us? Likely, too many to count. Microsoft has deemed it important to cue in users and admins to the status of the progress when commands and scripts are executed by including two cmdlets for Microsoft PowerShell (PS). These indicators may be integrated into existing scripts to provide some feedback on the status of a process.
Note: The aim of this article is to inform the script writer of the cmdlets and provide an idea of how each of them may be married to existing scripts. How they are married and to the degree of success will depend on the complexity of the script and the programming skill set of the author. As with anything computing-related, there are multiple ways to get something done. You just need to find what works best for you and your organization.
Write-Verbose
This single cmdlet is simple yet effective in providing a minimal status update, often relying on the inclusion of a single line to the script for each function you wish to be updated about.
Strength: It works with most scripts. It may be included as a single line of code or as several single code lines, one for each status update.
Weakness: Its simplicity also extends to its output capability. Often, only a single line is printed on-screen as the status update. Does not provide a progress bar or percentage complete natively.
Usage:
Write-Verbose -Message "Status Update: Command 01 Completed"
Write-Progress
This is the more robust and often preferred method of providing updates for PS experts. This method may be included as a few lines of code, wrapped as a function to be used repeatedly throughout a script, or mixed in with variables to minimize code duplication.
Strength: The amount of information provided to the user or admin is the biggest benefit of this cmdlet. It can create a progress bar to show a percentage of completion, even for multiple or nested cmdlets. Second is the ability to wrap this in a function for reference in future cmdlets or multiple times within the same script. It is especially useful for looped items, such as executing processes against multiple objects.
Weakness: The larger code base and flexibility make this a bit more complex to integrate with existing scripts. Depending on the aim of the author, this cmdlet may just be a bit of overkill for some instances. Nesting via IDs works well, but again the complexity could break existing script functionality if not tested thoroughly.
Usage:
for ($i = 0; $i -le 100; $i++ ) { Write-Progress -Activity "Perform Specific Task" -Id 1 -Status "$i% Complete:" -PercentComplete $i; }