While working in PowerShell, there will be times when you are required to print the value of variables for debugging or logging purposes. In this tutorial, I will explain how to print variables in PowerShell using different methods and examples.
To print a variable in PowerShell, you can use the Write-Output cmdlet, which sends the specified objects down the pipeline to the next command or displays them in the console if it’s the last command. For example, if you have a variable $city containing the value “New York,” you can print it by executing Write-Output $city. This will output “New York” to the console, making it a straightforward method for displaying variable values.
PowerShell Print Variable
There are different methods to print variables in PowerShell, including examples.
n PowerShell, variables are used to store data that can be used and manipulated throughout your scripts. Variables in PowerShell are denoted with a $ symbol followed by the variable name. For example:
$city = "New York"We will see how to print the value of this variable in PowerShell and some other examples.
1. Write-Output
The first command you can use to print the value of a variable is by using the Write-Output PowerShell cmdlet.
The Write-Output cmdlet sends the specified objects down the pipeline to the next command. If the command is the last in the pipeline, the console displays the objects.
Syntax:
Write-Output [-InputObject] <PSObject> [-NoEnumerate] [<CommonParameters>]Here is a basic example of using the Write-Output to print a variable in PowerShell.
$city = "New York"
Write-Output $cityThis will print “New York” to the console. I executed the above PowerShell script, and you can see the exact output in the screenshot below:

This cmdlet also provides various optional parameters, but these are a few useful parameters. So, let me show you some examples.
- -InputObject: Specifies the object to send to the pipeline. This positional parameter can be omitted if the object is the only argument. Here is an example.
city = "New York"
Write-Output -InputObject $cityThis will print “New York” to the console.
- -NoEnumerate: Prevents the cmdlet from enumerating the collection. By default,
Write-Outputenumerates items in a collection, sending each item individually to the pipeline.
Here is an example:
$cities = @("New York", "Los Angeles", "Chicago")
Write-Output -NoEnumerate $citiesThis will print the entire array ("New York", "Los Angeles", "Chicago") as a single object.
- Common Parameters: These parameters are standard across many cmdlets and include
-Verbose,-Debug,-ErrorAction,-ErrorVariable,-OutBuffer, and-OutVariable. Here is an example.
$city = "Houston"
Write-Output $city -VerboseThis will print “Houston” and include additional verbose output if enabled.
Read Set and Get Environment Variables in PowerShell
2. Write-Host
The Write-Host cmdlet in PowerShell displays text in the console. It’s often used to display messages directly to the user.
Syntax:
Write-Host [-Object] <Object> [-NoNewline] [-Separator <Object>] [<CommonParameters>]Here is a basic example.
$city = "Los Angeles"
Write-Host $cityThis will print “Los Angeles” to the console in the default color.
The output is in the screenshot below after I executed the PowerShell script above using VS code.

- -Object: Specifies the object to be written to the console. Here is an example:
$city = "Los Angeles"
Write-Host -Object $cityThis will print “Los Angeles” to the console in the default color.
- -NoNewline: Prevents the cmdlet from adding a newline character at the end of the output. Here is an example.
Write-Host -Object "Hello, " -NoNewline
Write-Host -Object "World!"This will print “Hello, World!” on the same line.
- -Separator: Specifies a string to use as the separator between objects. Here is an example.
$cities = @("New York", "Los Angeles", "Chicago")
Write-Host -Object $cities -Separator ", "This will print “New York, Los Angeles, Chicago”.
3. Write-Verbose
The Write-Verbose cmdlet in PowerShell writes text to the verbose message stream. You need to enable verbose output by using the parameter to see verbose messages.
Syntax:
Write-Verbose [-Message] <String> [<CommonParameters>]Here is a simple example.
$city = "Chicago"
Write-Verbose $city -VerboseThis will print “Chicago” only if verbose output is enabled. Here is the output in the screenshot below:

- -Message: Specifies the message to be written to the verbose stream. Here is an example.
$city = "Chicago"
Write-Verbose -Message $city -VerboseThis will print “Chicago” only if verbose output is enabled.
4. Write-Warning
The Write-Warning cmdlet in PowerShell prints a warning message to the console.
Syntax:
Write-Warning [-Message] <String> [<CommonParameters>]Here is a simple example.
$city = "Houston"
Write-Warning $cityThis will print “WARNING: Houston” to the console. Here is the output in the screenshot below:

5. Write-Error
The Write-Error cmdlet in PowerShell writes an error message to the error stream.
Syntax:
Write-Error [-Message] <String> [-Category <ErrorCategory>] [-ErrorId <String>] [-TargetObject <Object>] [<CommonParameters>]Here is a simple example:
$city = "Phoenix"
Write-Error $cityThis will print “Phoenix” as an error message. You can see the output in the screenshot below:

- -Message: Specifies the error message to be written.
$city = "Phoenix"
Write-Error -Message $cityThis will print “Phoenix” as an error message.
- -Category: Specifies the category of the error. Here is an example.
$city = "Phoenix"
Write-Error -Message $city -Category InvalidOperationThis will print “Phoenix” as an error message with the category “InvalidOperation”.
I hope you got to know now how to print a variable in PowerShell.
Check out How to Check if a Variable is Null in PowerShell?
Print Variable in PowerShell – Advanced Techniques
Now, let me show you some advanced techniques that you can use to format the output for printing variables in PowerShell.
Format Output
PowerShell provides various ways to format the output, making it more readable.
$city = "San Francisco"
$population = 883305
Write-Output ("The population of {0} is {1}" -f $city, $population)This will print “The population of San Francisco is 883305”. Here is the output you can see in the screenshot below:

Using Variables in Strings
You can also embed variables directly within strings in PowerShell.
$city = "San Diego"
Write-Output "Welcome to $city!"This will print “Welcome to San Diego!”.
Exporting to Files
Sometimes, you may need to save the output to a file rather than displaying it on the console.
$city = "Dallas"
$path = "C:\USA_Cities.txt"
Add-Content -Path $path -Value $cityThis will append “Dallas” to the file located at C:\USA_Cities.txt.
PowerShell Print Variable Examples
Now, let me show you two real examples of printing a variable in PowerShell.
Example 1: Printing State Capitals
Let me show you first how we can print some state capitals that is there is a variable.
$states = @{
"California" = "Sacramento"
"Texas" = "Austin"
"Florida" = "Tallahassee"
}
foreach ($state in $states.Keys) {
Write-Output "The capital of $state is $($states[$state])."
}This script will print the capitals of California, Texas, and Florida.
Example 2: Logging Weather Data
Here is another example where you want to log weather data.
$city = "Seattle"
$temperature = 75
$logPath = "C:\Weather_Log.txt"
Add-Content -Path $logPath -Value "The temperature in $city is $temperature degrees."
Write-Output "Weather data logged successfully."This script logs the temperature of Seattle to a file and confirms the action.
Conclusion
In this tutorial, I have explained how to print PowerShell variable using different methods like:
- Write-Output
- Write-Host
- Write-Verbose
- Write-Warning
- Write-Error
We saw how to use these methods with various examples. Do you still have any doubts? Feel free to leave a comment below. I will try to reply ASAP.
Bijay Kumar is an esteemed author and the mind behind PowerShellFAQs.com, where he shares his extensive knowledge and expertise in PowerShell, with a particular focus on SharePoint projects. Recognized for his contributions to the tech community, Bijay has been honored with the prestigious Microsoft MVP award. With over 15 years of experience in the software industry, he has a rich professional background, having worked with industry giants such as HP and TCS. His insights and guidance have made him a respected figure in the world of software development and administration. Read more.