SQL Clone
SQLServerCentral is supported by Redgate
Log in  ::  Register  ::  Not logged in

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-Debug Writes a debug message to the console. Uses $DebugPreference variable or -debug parameter
Write-Error Writes an error to the error stream.
Write-EventLog Writes an event to the event log. Requires PowerShell run as administrator
Write-Host Writes output to a host.
Write-Output Sends specified objects to next command in pipeline, or console if last command.
Write-Progress Displays a progress bar – does not display if $ProgressPreference = SilentlyContinue
Write-Verbose Writes text to the verbose message string. Uses $VerbosePreference or -Verbose on any command.
Write-Warning Writes 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
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

Sending output to other locations

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

Out-Null Deletes output instead of sending it down the pipeline
Out-Host Sends output to the command line
Out-File Sends output to a file
Out-Printer Sends output to a printer
Out-String Sends objects to the host as string.
Out-Default Sends output to the default formatter and to the default output cmdlet.
Out-GridView Sends 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



Leave a comment on the original post [blog.waynesheffield.com, opens in a new window]

Loading comments...