Introduction and use of the Write family cmdlet in PowerShell

Source: Internet
Author: User

Write related commands are mostly interacting with the host, such as: Output debugging information, output Error object, display progress information, control output effect, etc.

In many cases, we want to be able to export some information in a special color or background colors. In the PowerShell console, we can use the Write-host command to implement this feature. Parameter foregroundcolor is used to specify the font color, backgroundcolor to specify the color of the background. For example:

PS c:\> write-host-backgroundcolor black-foregroundcolor Red "Hello world!"

Hello world!

What should you do if you can't remember what color you have? Simply enter a nonexistent color after the argument, for example:

PS C:\> Write-Host -BackgroundColor NoSuchColor
Write-Host : Cannot bind parameter 'BackgroundColor'. Cannot convert value "NoSuchColor" to type "System.ConsoleColor"
due to invalid enumeration values. Specify one of the following enumeration values and try again. The possible enumerat
ion values are "Black, DarkBlue, DarkGreen, DarkCyan, DarkRed, DarkMagenta, DarkYellow, Gray, DarkGray, Blue, Green, Cy
an, Red, Magenta, Yellow, White".
At line:1 char:28
+ Write-Host -BackgroundColor <<<< NoSuchColor

We can see that the PowerShell error message already contains all the valid values for the parameter. With this approach, you can often avoid looking at manuals or checking other data to determine which values are supported by parameters.

Write-debug, write-warning, and Write-verbose are three very similar commands that output a single message to the console, primarily for scripting people to control output more flexibly. Traditional shell and scripting languages often help authors to troubleshoot script errors by printing some debugging information to the screen. PowerShell provides a powerful debugging capability, but also preserves the ability to debug this classic method with print messages by providing write-debug.

In the case of a copy operation, if one already exists, you may not want to stop the copy operation, but expect PowerShell to provide a warning message. Use Write-verbose at this time. If you use compression software, we may be concerned about the compression of the time, which files are packaged, we often want a program to execute when the thread more detailed message. We can use Write-verbose to achieve this goal.

These write commands are not actually used very often, so I'll put them in a later section that specifically describes how to write a robust script.

If you are writing a copy file or executing an installation script, providing a progress bar will allow the user to wait for a better experience when the script executes. PowerShell designers have thought about these details for us and can use write-progress to do so.

Let's take a look at one of the simplest progress bar models:

PS c:\> for ($i = 0; $i-lt $i + +) {write-progress-activity "Learning PowerShell"-status "percentage: $i"-perce Ntcomplete $i; Start-sleep-milliseconds 50}

You will see on the screen:

Learning PowerShell

percentage:61

[Ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo]

We use parameter-activity to set a name for the entire progress bar (the name of the activity), where we define "learing PowerShell", where the parameter-status is used to indicate the state of the current execution throughout the process, where we simply hint at the percentage of the current completion.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.