http://www.sqlservercentral.com/blogs/discussionofsqlserver/2013/02/07/a-month-of-powershell-day-7-script-inputoutput/

Printed 2014/07/25 05:38AM

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

By Wayne Sheffield, 2013/02/07

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:

Source code   
$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.

Source code   
$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-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:

Source code   
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

 


Copyright © 2002-2014 Simple Talk Publishing. All Rights Reserved. Privacy Policy. Terms of Use. Report Abuse.