EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 360+ Courses All in One Bundle
  • Login
Home Data Science Data Science Tutorials PowerShell Tutorial Powershell Write-Host
Secondary Sidebar
PowerShell Tutorial
  • cmdlet
    • PowerShell Wait
    • PowerShell Match
    • cmdlets in PowerShell
    • Start PowerShell from cmd
    • Add-Content in PowerShell
    • Get Help in PowerShell
    • PowerShell Copy-Item
    • PowerShell Remove-Item
    • PowerShell Move-Item
    • Get Command in PowerShell
    • PowerShell Run Command
    • Windows PowerShell ISE
    • Windows Powershell Commands
    • WinRM PowerShell
    • PowerShell Date
    • Powershell Write-Host
    • PowerShell Get-ChildItem
    • PowerShell Sort-Object
    • PowerShell Where Object
    • PowerShell Set-Content
    • PowerShell Set-Location
    • PowerShell Invoke-Command
    • PowerShell Invoke-Webrequest
    • PowerShell Get-Location
    • PowerShell Get-Date
    • PowerShell Get-Service
    • PowerShell Test-Path
    • Powershell Module Path
    • PowerShell Out-File
    • PowerShell if File Exists
    • Powershell Copy File
    • PowerShell Delete File
    • PowerShell New-Item
    • PowerShell Rename-Item
    • PowerShell ComputerName
    • PowerShell Get-Content
    • PowerShell Get-Item
    • PowerShell Get-ADUser
    • PowerShell Grep
    • PowerShell Concatenate String
    • PowerShell Get-Process
    • PowerShell Count
    • PowerShell pause
  • Basics
    • PowerShell Restart Service
    • PowerShell comment
    • PowerShell Map Network Drive
    • PowerShell Append to File
    • PowerShell print
    • What is PowerShell
    • Uses Of Powershell
    • PowerShell Empire
    • PowerShell Parameter
    • PowerShell Stop Service
    • PowerShell Versions
    • How To Install PowerShell
    • PowerShell uninstall module
    • How to Use PowerShell?
    • PowerShell Logging
    • PowerShell Tools
    • PowerShell Commands
    • PowerShell Version Command
    • PowerShell Administrator
    • PowerShell Modules
    • PowerShell Registry
    • PowerShell block Comment
    • PowerShell Verbs
    • PowerShell list
    • PowerShell add user to group
    • PowerShell Write to Console
    • Variable in PowerShell
    • PowerShell New Line
    • PowerShell prompt for input
    • PowerShell File Extension
    • Powershell Remotesigned
    • PowerShell Write to File
    • PowerShell Ping
    • PowerShell wget
    • PowerShell Global variable
    • PowerShell Get-ADGroup
    • Array in PowerShell
    • PowerShell Multidimensional Array
    • PowerShell Array of Strings
    • PowerShell? join array
    • Useful PowerShell Scripts
    • String in PowerShell
    • PowerShell Switch Statement
    • PowerShell Function Parameters
    • PowerShell vs PowerShell ISE
    • PowerShell test-connection
    • PowerShell Test-NetConnection
    • PowerShell GUI
    • PowerShell Variable in String
    • PowerShell Active Directory
  • Variables
    • PowerShell Variables
    • PowerShell Environment Variables
    • PowerShell set environment variable
    • Hashtable in PowerShell
    • Set Variable in PowerShell
  • Operators
    • PowerShell Operators
    • Comparison Operators in PowerShell
    • Logical Operators in PowerShell
    • PowerShell Boolean
    • PowerShell Like Operator
  • Control Statements
    • If Statement in PowerShell
    • If Else in PowerShell
    • Else If in PowerShell
    • Loops in PowerShell
    • For loop in PowerShell
    • PowerShell While Loop
    • PowerShell do while
    • PowerShell Loop through Array
    • PowerShell add to array
    • PowerShell ForEach Loop
    • PowerShell Break
    • PowerShell Continue
    • Switch Case in PowerShell
    • PowerShell If-Not
    • Try-catch in PowerShell
  • Functions
    • PowerShell Functions
    • PowerShell String Functions
    • powershell nslookup
    • PowerShell here string
    • PowerShell Wildcards
    • Regex in PowerShell
    • PowerShell not like
    • PowerShell Filter
    • PowerShell Sleep
    • PowerShell where
    • PowerShell join string
    • PowerShell Exit
    • PowerShell null
    • PowerShell Dictionary
    • PowerShell Location
    • PowerShell Start-Service
    • PowerShell is not digitally signed
    • PowerShell Uptime
    • PowerShell Create Directory
    • PowerShell Trim
    • PowerShell Join-Path
    • PowerShell Execution Policy
    • PowerShell SubString
    • PowerShell Format Table
    • PowerShell Import Module
    • PowerShell ForEach Object
    • PowerShell Alias
    • PowerShell Scheduled Task
    • PowerShell Convert String to Date
    • PowerShell Split String
    • PowerShell Multiline String
    • PowerShell MultiLine Comment
    • PowerShell Rename Folder
    • PowerShell Delete Folder
    • PowerShell String Replace
    • PowerShell join
    • PowerShell xcopy
    • PowerShell Base64
    • PowerShell Tail
    • PowerShell User List
    • PowerShell remove User from group
    • PowerShell JSON Format
    • PowerShell Send Mail
    • PowerShell Convert to String
    • PowerShell Start-Process
    • PowerShell change directory
    • PowerShell Open File
    • PowerShell Batch File
    • PowerShell ZIP
    • PowerShell unzip
    • PowerShell XML
    • PowerShell XML Parsing
    • Remote PowerShell
    • PowerShell Escape Character
    • PowerShell scriptblock
    • PowerShell Executable Location
    • PowerShell Import-CSV?
    • PowerShell Export CSV
  • Interview Questions
    • PowerShell Interview Questions

Related Courses

Shell Scripting Course

All in One Data Science Courses

Data Visualization Courses

Powershell Write-Host

By Chirag NagarekarChirag Nagarekar

Powershell-Write-Host

Introduction to Powershell Write-Host

The write-host cmdlet is used to display the output in the PowerShell console itself. You can customize the displayed output with various font colors and background colors. When the object is passed to the pipeline Write-host does not take it as an object instead, it considers it as a name parameter or value and simply displays output in the console.

 Syntax

Write-Host
[[-Object] <Object>] [-NoNewline] [-Separator <Object>] [-ForegroundColor <ConsoleColor>] [-BackgroundColor <ConsoleColor>] [<CommonParameters>]

Parameters

  • –Object: It is used to display in console output. Generally, the object is a string object, variable or can be operational values.
  • –NoNewLine: With this parameter, the output will be continued to the same line, not in the new line.
  • –Separator: You can separate words in the bracket with a specified character or word. An explanation is given later in examples.
  • –ForegroundColor: You can set the color of the text while displaying output. There is no default color.

The Colors are as below:

All in One Data Science Bundle(360+ Courses, 50+ projects)
Python TutorialMachine LearningAWSArtificial Intelligence
TableauR ProgrammingPowerBIDeep Learning
Price
View Courses
360+ Online Courses | 50+ projects | 1500+ Hours | Verifiable Certificates | Lifetime Access
4.7 (86,354 ratings)
-Black -DarkBlue
-DarkGreen -DarkCyan
-DarkRed -DarkMagenta
-Darkyellow -Gray
-DarkGray -Blue
-Green -Cyan
-Red -Magenta
-Yellow -White
  • –Background Color: This parameter specifies the background color of the text. There is no default color. The colors are as below.
-Black -DarkBlue
-DarkGreen -DarkCyan
-DarkRed -DarkMagenta
-DarkYellow -Gray
-DarkGray -Blue
-Green -Cyan
-Red -Magenta
-Yellow -White
  • <CommonParameters>: This cmdlet supports the common parameters.Verbose, Debug, ErrorAction, ErrorVariable, WarningAction, WarningVariable, OutBuffer, PipelineVariable and OutVariable.

Examples of Powershell Write-Host

Below given are some examples of Powershell write-host:

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

Example #1: Write-Host with Object

Write-Host "This is a Simple String"

Output:

Host with object

Similarly, you can write the same with passing string to the Pipeline and the output will be the same as above.

"This is a simple string" | Write-Host

Now suppose, we are passing the multiple objects together. Let’s consider Get-Service to the write-host.

Get-Service | Select -First 10 | Write-Host

Output:

powershell Write- Host 4

But when you use Write-Output

Get-Service | Select -First 10 | Write-Output

Output:

powershell Write- Host 5

So, write-host only writes the name of the service, not the entire Get-Service output because Write-Host cmdlet can’t process the entire output as a single object but the single value or string. You can display all default values using the Write-Output Parameter.

Example #2: Write-Host with –NoNewLine

By default, when the write-host command is executed at the end new line parameter is also executed so whatever you write output will be in the next line but using –NoNewLine parameter, the output will be concatenated.

Write-Host "This is first line..." -NoNewline
Write-Host "There is no space"

Output:

no new line

Example #3: Write-Host with –Separator

With the separator parameter, you can put a specific character or word between each value. For example,

Write-Host (1,2,3,4,5) -Separator "->"

Output:

Separator

Write-Host ("This","is","string") -Separator ","

powershell op 9

You can also separate values with a specific word.

Write-Host ("This","is","string") -Separator " hi "

Separator won’t work with a single value or string.

Write-Host "This is string" -Separator "->"

Seperator

Note: Separator is not to replace a specific word.

Example #4: Write-Host with –Foregroundcolor

Sometimes you need to display output text with a specific color. You can use different colors to display text.

Write-Host "This is magenta foreground text" -ForegroundColor Magenta

Output:

Foregroundcolor

Write-Host "This is Dark cyan foreground text" -ForegroundColor DarkCyan

powershell new 12

Example #5: Write-Host with –Backgroundcolor

You can change the background color of the text with this parameter.

Write-Host "This is white text with Dark Red background"-
BackgroundColor DarkRed

Backgroundcolor

Write-Host "Magenta text with green background" -ForegroundColor Magenta -
BackgroundColor green

powershell new 14

Note: Background parameter doesn’t change the console color, it just changes the background color of the text it displaying.

Conclusion

Write-host can’t capture or store the output, it simply displays the output. To capture the output you need to use the write-output cmdlet but with the latter command, you can’t use the different colors.

For example, in the below command output will not be stored in test.txt and instead, it will be displayed in the PowerShell console.

Write-Host "Simple text" > c:\test.txt

But when you use write-output you can store in a text file.

Write-Output "Simple text" > c:\test.txt

Recommended Articles

This has been a guide to Powershell Write-Host. Here we discuss the syntax, parameters, and various examples of Powershell Write-Host with appropriate code and output. You may also have a look at the following articles to learn more –

  1. Array in PowerShell
  2. PowerShell ForEach Loop
  3. Add-Content in PowerShell
  4. Ansible Commands
Popular Course in this category
Programming Languages Training (41 Courses, 13+ Projects, 4 Quizzes)
  41 Online Courses |  13 Hands-on Projects |  322+ Hours |  Verifiable Certificate of Completion
4.5
Price

View Course

Related Courses

Shell Scripting Training (4 Courses, 1 Project)4.9
All in One Data Science Bundle (360+ Courses, 50+ projects)4.8
Data Visualization Training (15 Courses, 5+ Projects)4.7
1 Shares
Share
Tweet
Share
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2022 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Data Science Course

SPSS, Data visualization with Python, Matplotlib Library, Seaborn Package

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more