Blog Post

A Month of PowerShell – Day 7 (Script Input/Output)

,

Welcome to Day 7 of my “A Month of PowerShell” series. This series will use the series landing page on this blog at http://blog.waynesheffield.com/wayne/a-month-of-powershell/. Please refer to this page to see all of the posts in this series, and to quickly go to them.

Sending messages to the user

PowerShell provides several different cmdlets to allow the PowerShell script to send informational messages to the user. This table shows several of them:

Write-DebugWrites a debug message to the console. Uses $DebugPreference variable or -debug parameter
Write-ErrorWrites an error to the error stream.
Write-EventLogWrites an event to the event log. Requires PowerShell run as administrator
Write-HostWrites output to a host.
Write-OutputSends specified objects to next command in pipeline, or console if last command.
Write-ProgressDisplays a progress bar – does not display if $ProgressPreference = SilentlyContinue
Write-VerboseWrites text to the verbose message string. Uses $VerbosePreference or -Verbose on any command.
Write-WarningWrites a warning message.

There are many optional parameters for these, so don’t forget to read the help for the cmdlets. A quick example of several of these:

$DebugPreference = "SilentlyContinue"
Write-Debug "A fantastic, helpful debug message"
Write-Debug "A fantastic, helpful debug message" -Debug
$DebugPreference = "Continue"
Write-Debug "A fantastic, helpful debug message"
$DebugPreference = "SilentlyContinue"
 
$File = "X:\temp\output.txt"
if (!(Test-Path $File)) {Write-Error "File $File not found!"}
 
Write-Host "Here's a message for you!"
 
Write-Warning "Here's a colored message for you!"
 
Write-Verbose "Searching the Application Event Log" -Verbose
 
$ProgressPreference
for ($i = 1; $i -le 100; $i++)
{
    Write-Progress "Search in Progress" -Status "$i% Complete:" -PercentComplete $i;
}

Getting Input from the user

The Read-Host cmdlet allows the script to prompt the user for input, and it waits until text has been entered.

$Input = Read-Host -Prompt "enter some text"
$Pwd   = Read-Host -Prompt "Enter your password" -AsSecureString
 
$Input
$Pwd

Sending output to other locations

PowerShell provides several cmdlets to pipe output to other locations as well, as the following table shows:

Out-NullDeletes output instead of sending it down the pipeline
Out-HostSends output to the command line
Out-FileSends output to a file
Out-PrinterSends output to a printer
Out-StringSends objects to the host as string.
Out-DefaultSends output to the default formatter and to the default output cmdlet.
Out-GridViewSends output to an interactive table in a separate window

The following script will send all of the processes to the appropriate output:

Get-Process | Out-Null
Get-Process | Out-Host
Get-Process | Out-File $env:TEMP\ProcessList.txt
Get-Process | Out-Printer
Get-Process | Out-String
Get-Process | Out-Default
Get-Process | Out-GridView

 

Rate

You rated this post out of 5. Change rating

Share

Share

Rate

You rated this post out of 5. Change rating