EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign Up
Home Data Science Data Science Tutorials PowerShell Tutorial PowerShell Write to Console
 

PowerShell Write to Console

Updated March 6, 2023

PowerShell Write to Console

 

 

Definition of PowerShell Write to Console

PowerShell Write to console commands purpose are to write the Information, data, outputs, progress, verbose, variables, warnings on the console where we can use the different colors to decorate the displayed output on the console, ask for the user input, write the error messages on the console when something goes wrong with script or command, write the verbose to display the command process running the background and write information for the user attention.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

Syntax:

There are various commands are used for the console display and they are as below.

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

Write-Output
[-InputObject] <PSObject[]>
[-NoEnumerate] [<CommonParameters>]

Write-Warning
[-Message] <String>
[<CommonParameters>]

Write-Verbose
[-Message] <String>
[<CommonParameters>]

Write-Information
[-MessageData] <Object>
[[-Tags] <String[]>] [<CommonParameters>]

Read-Host
[[-Prompt] <Object>] [-MaskInput] [<CommonParameters>]

Read-Host
[[-Prompt] <Object>] [-AsSecureString] [<CommonParameters>]

How Write to console works in PowerShell?

PowerShell has various methods to write the output or the customized output on the display console. When we write the line directly on the PowerShell console it displays that string. For example,

"This is a display output"

Output:

PowerShell Write to Console 1

When we concatenate the strings,

"This is a display output" + " This is a string"

Output:

PowerShell Write to Console 2

So basically when we don’t use any command to display the output, it automatically pipelines the output to the Out-Host command. So the first input is similar to,
“This is a display output” | Out-Host

Output:

PowerShell Write to Console 3

Again, when we use any cmdlet which displays the output on the console, it by default uses the hidden cmdlet Out-Host to display the output on the console. The Command output is pipelined to the Out-Host for the display but this process is hidden.

Get-Process

Output:

get process

The above command is similar to the,

Get-Process | Out-Host

And there are other commands like Write-Host, Write-Output, Write-Error, Write-Verbose, etc shown in the syntax which can customize the output like Foreground or background color specific, user inputs, writing verbose to display on the console.

Examples

More explanation is given in the examples below.

Example #1: Using Write-Output to write the output on the console.

Write-Output command is used to write the output on the PowerShell console.

Write-Output "This is a PowerShell"

Output:

PowerShell Write to Console 5

We can also store the string inside the variable and write the output.

$str = "This is a PowerShell Output"
Write-Output $str

Output:

PowerShell Write to Console 6

It also takes the pipeline input.

"This is a PowerShell" | Write-Output

Output:

write-output

If you store the output of the Write-Output command to the file, it won’t display the output on the console.

Write-Output "This is a PowerShell" | Out-File C:\Temp\output.txt

Output:

output

This command is useful for writing on the console where we can’t use the Write-Host like, PowerShell workflows and Azure PowerShell runbooks.

Example #2: Using Write-Host to write output on the console.

The Write-Host command is to display the output on the console with the decorative background and the foreground colors.

Write-Host "This is a PowerShell"

Output:

this is powershell

With the different background and the foreground color.

Write-Host "This is a PowerShell" -BackgroundColor Blue -ForegroundColor White

Output:

PowerShell Write to Console 7

We can also pipeline the output.

"This is a PowerShell" | Write-Host -BackgroundColor DarkGray -ForegroundColor DarkGreen

Output:

output 1

If we try to store the output to the file, this command won’t let you that but it will still display the output on the console.

Write-Host "This is a PowerShell" -BackgroundColor Blue -ForegroundColor White | Out-File C:\Temp\Hostout.txt

Output:

output 2

The above output does not store anything in the Hostout.txt file. It will be blank.

Example #3: Write-Verbose to write the output on the console.

Verbose is the message stream used to write the command processing information on the display but it needs the special parameter -Verbose to display the message. For example,

The below line will not display anything because we are not using the -Verbose parameter.

Write-Verbose "This is the PowerShell"

Output:

example 3

But once we add the -verbose parameter,

Write-Verbose "This is the PowerShell" -Verbose

Output:

example 3-1

Almost all the cmdlets in PowerShell use the -Verbose parameter so that we don’t need to write every information.

$services = "Spooler", "wuauserv"
Get-service $services | Stop-Service -Force -Verbose

Output:

example 3-2

Example #4: Write-Error to display the error on the console.

Write-Error message is useful for writing error messages on the console.

$service = 'Spooler1'
if(Get-Service -Name $service -EA Ignore){
Write-Output "$service is exist"
}
else{
Write-Error "$service doesn't exist"
}

Output:

example 4

You can also use the different parameters associated with the Write-Error command, like Category, ErrorID, Exception, etc.

Write-Error -Message "Error: Too many input values." -Category InvalidArgument -ErrorId B1

Output:

example 4-1

Example #5: Read-Host command to write on the console.

The Read-Host command takes the input from the user and it passes to another cmdlet. We can also use this command to display the message on the console after it is stored. For example,

Read-Host "Enter Message"
Enter Message: This is a PowerShell input

Output:

example 5

Example #6: Using System.Console class to write on the console.

We can use the .Net class System. Console to write the output on the console and for that, it uses the methods like

Write() and WriteLine().
[System.Console]::Write('This is a PowerShell')

Output:

example 6

Using WriteLine() method.
[System.Console]::WriteLine( "(d) Short date: . . . . . . . {0:d}`n" +
"(D) Long date:. . . . . . . . {0:D}`n" +
"(t) Short time: . . . . . . . {0:t}`n" +
"(T) Long time:. . . . . . . . {0:T}`n" +
"(f) Full date/short time: . . {0:f}`n" +
"(F) Full date/long time:. . . {0:F}`n"
, (Get-Date))

Output:

example 7

Conclusion

PowerShell has introduced many useful commands for writing on the console and that depends on the nature of the work. For example, let say if we want to display the error message, we can set either the background color of the text to Red or use the Write-Error command. If you want to write a script that can display verbose then use the Write-Verbose command and so on.

Recommended Articles

This is a guide to PowerShell Write to Console. Here we discuss definition, syntax, and How Write to console works in PowerShell?. You may also have a look at the following articles to learn more –

  1. PowerShell Wait
  2. PowerShell change directory
  3. PowerShell where
  4. PowerShell uninstall module

Primary Sidebar

Footer

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

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

EDUCBA

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

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA

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

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

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

EDUCBA Login

Forgot Password?

🚀 Limited Time Offer! - 🎁 ENROLL NOW