Skip to main content

Displaying information on the host with PowerShell

PowerShell scripters have a variety of methods that they can use to display information on the host.  Before we begin this lesson, remember that PowerShell should send an object as it’s output to the PowerShell pipeline.  In many cases, the author of a cmdlet may want to display other information.  This can include debugging or progress information.  Here are a few cmdlets and some information to help you understand their usage.

Write-Output

This is the proper way of sending output into the PowerShell pipeline.  Remember that this should be the object that is the end result of the code that was just executed.  This is also how we return information from a function to its calling statement.

Write-Host

This is the easiest cmdlet to use to instantly display information on the host.  The –ForeGroundColor and –BackGroundColor parameters allow you to use 16 different colors to emphasis your displaying information. 

Example:

PS C:\> Write-Host "Hello World" -ForegroundColor Red -BackgroundColor DarkYellow

Hello World

Write-Debug

This cmdlet is dependent on the setting of the environmental variable $DebugPreference. By default, it is set to SilentlyContinue.  By setting this variable to Continue, you will be able to write debug information to the host.  The advantage of this cmdlet is that it does not display information by default.  That means you can leave your debug comments in your script for later use and the user will not see them when the cmdlet is executed unless the user sets their $DebugPreference$ to continue.

Write-Host "Here is some code"

$Debugpreference = 'Continue'

Write-Debug "Here is the debug information"

$Debugpreference = 'SilentlyContinue'

Write-Debug "This debug message will not display."

Write-Host "End of Script"

 

Here is some code

DEBUG: Here is the debug information

End of Script

You can see that once the $DebugPreference variable was set to SilentlyContinue, the second debug message is not displayed.

One of the common parameters is –Debug.  By using this common parameter, you can turn on debug messages.  This switch will override the value of $DebugPreference value.  The difference is that it will pause script execution at each debug message.  Since the script is still running in memory, you can examine the contents of memory by placing the script in Suspend mode.

Function Test

{

[cmdletbinding()]

Param()

Write-Host "Here is some code"

Write-Debug "Here is the debug information"

Write-Debug "This debug message will not display."

Write-Host "End of Script"

}

image

Write-Verbose

This write cmdlet is controlled by the $VerbosePreference environmental variable.  It is also set to SilentlyContinue.  This one is actually a common parameter.  Talk a look at the help file About_CommonParameters to learn more.   This means that you can use the –Verbose switch with your cmdlet to see this information.  This cmdlet is best used to provide progress information for the cmdlets execution.

Function Test

{

[cmdletbinding()]

Param ([Switch]$PassThru)

 

Write-host "Hello"

Write-Verbose "World"

}

 

PS C:\> Test

Hello

 

PS C:\> Test -Verbose

Hello

VERBOSE: World

 

 

Write-Warning

This cmdlet can be used to write a warning to the host.  It is controlled by the $WarningPreference variable.  It is set to Continue by default.

PS C:\> Write-Warning "This is a Warning Meassage"

WARNING: This is a Warning Meassage

 

PS C:\> $Var1 = "This is also a message"

 

PS C:\> $Var2 = "And so is this one."

 

PS C:\> $Var1, $Var2 | Write-Warning

WARNING: This is also a message

WARNING: And so is this one.

 

Write-Error

Write-Error is a bit more complex to use.  The detailed help file for this cmdlet provides more detailed information on how to use this cmdlet.  Write-Error places an object into the Error stream.  Look at this example.

 

PS C:\> Write-error "Hello"

Write-error "Hello" : Hello

    + CategoryInfo          : NotSpecified: (:) [Write-Error], WriteErrorException

    + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException

It is not as pretty to look at.  It does place this information into the $Error array.

PS C:\> $Error[0]

Write-error "Hello" : Hello

    + CategoryInfo          : NotSpecified: (:) [Write-Error], WriteErrorException

    + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException

Before using this cmdlet, take come time to study the help file on it so you can use it properly.

To get a list of all write cmdlets:

PS C:\> Get-Command write*

 

CommandType Name                  ModuleName                    

----------- ----                  ----------                    

Alias       write -> Write-Output                               

Function    Write-Quantification  PSv3                          

Cmdlet      Write-Debug           Microsoft.PowerShell.Utility  

Cmdlet      Write-Error           Microsoft.PowerShell.Utility  

Cmdlet      Write-EventLog        Microsoft.PowerShell.Management

Cmdlet      Write-Host            Microsoft.PowerShell.Utility  

Cmdlet      Write-Output          Microsoft.PowerShell.Utility  

Cmdlet      Write-Progress        Microsoft.PowerShell.Utility  

Cmdlet      Write-Verbose         Microsoft.PowerShell.Utility  

Cmdlet      Write-Warning         Microsoft.PowerShell.Utility  

Application write.exe                                           

Application write.exe                   

                          

To get a list of the variable that control the output of several of these cmdlets:

 

PS C:\> Get-Variable *Preference

 

Name                  Value          

----                  -----          

ConfirmPreference     High           

DebugPreference       SilentlyContinue

ErrorActionPreference Continue       

ProgressPreference    Continue       

VerbosePreference     SilentlyContinue

WarningPreference     Continue       

WhatIfPreference      False 

 

 

Comments

Popular posts from this blog

Adding a Comment to a GPO with PowerShell

As I'm writing this article, I'm also writing a customization for a PowerShell course I'm teaching next week in Phoenix.  This customization deals with Group Policy and PowerShell.  For those of you who attend my classes may already know this, but I sit their and try to ask the questions to myself that others may ask as I present the material.  I finished up my customization a few hours ago and then I realized that I did not add in how to put a comment on a GPO.  This is a feature that many Group Policy Administrators may not be aware of. This past summer I attended a presentation at TechEd on Group Policy.  One organization in the crowd had over 5,000 Group Policies.  In an environment like that, the comment section can be priceless.  I always like to write in the comment section why I created the policy so I know its purpose next week after I've completed 50 other tasks and can't remember what I did 5 minutes ago. In the Group Policy module for PowerShell V3, th

Return duplicate values from a collection with PowerShell

If you have a collection of objects and you want to remove any duplicate items, it is fairly simple. # Create a collection with duplicate values $Set1 = 1 , 1 , 2 , 2 , 3 , 4 , 5 , 6 , 7 , 1 , 2   # Remove the duplicate values. $Set1 | Select-Object -Unique 1 2 3 4 5 6 7 What if you want only the duplicate values and nothing else? # Create a collection with duplicate values $Set1 = 1 , 1 , 2 , 2 , 3 , 4 , 5 , 6 , 7 , 1 , 2   #Create a second collection with duplicate values removed. $Set2 = $Set1 | Select-Object -Unique   # Return only the duplicate values. ( Compare-Object -ReferenceObject $Set2 -DifferenceObject $Set1 ) . InputObject | Select-Object – Unique 1 2 This works with objects as well as numbers.  The first command creates a collection with 2 duplicates of both 1 and 2.   The second command creates another collection with the duplicates filtered out.  The Compare-Object cmdlet will first find items that are diffe

How to list all the AD LDS instances on a server

AD LDS allows you to provide directory services to applications that are free of the confines of Active Directory.  To list all the AD LDS instances on a server, follow this procedure: Log into the server in question Open a command prompt. Type dsdbutil and press Enter Type List Instances and press Enter . You will receive a list of the instance name, both the LDAP and SSL port numbers, the location of the database, and its status.