EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 360+ Courses All in One Bundle
  • Login

PowerShell Write to Console

Home » Data Science » Data Science Tutorials » PowerShell Tutorial » PowerShell Write to Console

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.

Syntax:

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

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

Popular Course in this category
Sale
Shell Scripting Training (4 Courses, 1 Project)4 Online Courses | 1 Hands-on Project | 18+ Hours | Verifiable Certificate of Completion | Lifetime Access
4.5 (9,060 ratings)
Course Price

View Course

Related Courses
All in One Data Science Bundle (360+ Courses, 50+ projects)Data Visualization Training (15 Courses, 5+ Projects)

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

All in One Data Science Bundle (360+ Courses, 50+ projects)

360+ Online Courses

50+ projects

1500+ Hours

Verifiable Certificates

Lifetime Access

Learn More

0 Shares
Share
Tweet
Share
Primary Sidebar
PowerShell Tutorial
  • Basics
    • PowerShell comment
    • PowerShell Map Network Drive
    • PowerShell Append to File
    • PowerShell print
    • What is PowerShell
    • Uses Of Powershell
    • 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 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
  • 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
  • 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

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

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

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
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 Login

Forgot Password?

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.

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.

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

Special Offer - Shell Scripting Course Learn More