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

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.

How to run GPResult on a remote client with PowerShell

In the past, to run the GPResult command, you would need to either physically visit this client, have the user do it, or use and RDP connection.  In all cases, this will disrupt the user.  First, you need PowerShell remoting enabled on the target machine.  You can do this via Group Policy . Open PowerShell and type this command. Invoke-Command –ScriptBlock {GPResult /r} –ComputerName <ComputerName> Replace <ComputerName> with the name of the target.  Remember, the target needs to be online and accessible to you.

Error icon when creating a GPO Preference drive map

You may not have an error at all.  Take a look at the drive mapping below. The red triangle is what threw us off.  It is not an error.  It is simply a color representation of the Replace option of the Action field in the properties of the drive mappings. Create action This give you a green triangle. The Create action creates a new mapped drive for users. Replace Action The Replace action gives you a red triangle.  This action will delete and recreate mapped drives for users. The net result of the Replace action is to overwrite all existing settings associated with the mapped drive. If the drive mapping does not exist, then the Replace action creates a new drive mapping. Update Action The Update action will have a yellow triangle. Update will modify settings of an existing mapped drive for users. This action differs from Replace in that it only updates settings defined within the preference item. All other settings remain as configured on the mapped drive. If the