Output Variables in PowerShell

As a PowerShell developer, you need to display output the value of variables. In this tutorial, I will explain how to output variables in PowerShell using different methods and examples.

To output a variable in PowerShell using the Write-Output cmdlet, simply pass the variable as an argument to the cmdlet. For example, if you have a variable $processInfo that contains information about running processes, you can display this information in the console by executing Write-Output $processInfo.

Output Variables in PowerShell

In PowerShell, a variable is a unit of memory where values are stored. Variables are represented by text strings that begin with a dollar sign ($). For example:

$greeting = "Hello, PowerShell!"

In this example, $greeting is a variable that stores the string “Hello, PowerShell!”.

Now, let me show you output variables in PowerShell using different methods.

1. Write-Output

The Write-Output cmdlet is the most common way to display the value of a variable in PowerShell. It writes the variable’s value to the output stream, which can be displayed in the console or passed to another cmdlet.

Example:

$processInfo = Get-Process
Write-Output $processInfo

In this example, the Get-Process cmdlet retrieves information about running processes, and Write-Output displays this information in the console.

Here is the output of the above script; check out the screenshot below:

Output Variables in PowerShell

Let me show you another real-time example.

Real-Time Example: Monitoring Disk Space

$diskSpace = Get-PSDrive -PSProvider FileSystem
Write-Output $diskSpace

This script retrieves disk space information and outputs it to the console.

Check out Check if a Variable is Empty in PowerShell

2. Write-Host

The Write-Host cmdlet writes directly to the console, making it useful for displaying messages or variable values directly to the user. It is often used for debugging or providing user feedback.

Example:

$userName = "John Doe"
Write-Host "The current user is $userName"

This script outputs the message “The current user is John Doe” directly to the console.

Here is the output in the screenshot below:

powershell output variable

Here is another example.

Real-Time Example: User Login Notification

$userName = "Jane Smith"
Write-Host "User $userName has logged in."

This script uses Write-Host to display a login message.

Read Reset Variables in PowerShell

3. Write-Debug

The Write-Debug cmdlet in PowerShell is used to display debug messages. It is particularly useful when you want to output variable values for debugging purposes without displaying them to the end user.

Example:

$debugInfo = "This is a debug message."
Write-Debug $debugInfo

To see the debug message, you need to enable debugging by running:

$DebugPreference = "Continue"

Real-Time Example: Debugging a Script

Let me show you another example: most developers use it for debugging.

$step1 = "Step 1 completed."
Write-Debug $step1

$step2 = "Step 2 completed."
Write-Debug $step2

$finalStep = "All steps completed."
Write-Debug $finalStep

By enabling debugging, you can see the progress of each step in the console.

Check out PowerShell Static Variables

4. Write-Verbose

The Write-Verbose cmdlet in PowerShell is used to display verbose messages, which provide additional details about the script’s operation. This is useful for understanding the script’s flow and for troubleshooting.

Example:

$verboseMessage = "This is a verbose message."
Write-Verbose $verboseMessage

To see verbose messages, you need to enable verbose output by running:

$VerbosePreference = "Continue"

Real-Time Example: Verbose Logging

$taskStatus = "Task completed successfully."
Write-Verbose $taskStatus

This script provides detailed task status messages.

Read Check if a Variable Exists in PowerShell

5. Out-File

The Out-File cmdlet in PowerShell sends the output to a file instead of the console. This is useful for logging or saving the output for later analysis.

Example:

$logData = "This is a log entry."
Out-File -FilePath "C:\Logs\log.txt" -InputObject $logData

This script writes the log entry to a file named log.txt in the C:\Logs directory.

Here is the output in the screenshot below:

write variable output in powershell

Real-Time Example: Logging Disk Space Information

Here is another example to help you understand it better.

$diskSpace = Get-PSDrive -PSProvider FileSystem
Out-File -FilePath "C:\Logs\diskspace.txt" -InputObject $diskSpace
Write-Host "Disk space information has been logged."

In this script, Get-PSDrive retrieves the disk space information, and Out-File writes it to diskspace.txt. Write-Host provides feedback to the user.

Conclusion

Outputting variables in PowerShell is a fundamental task that can be accomplished using various cmdlets such as Write-Output, Write-Host, Write-Debug, Write-Verbose, and Out-File. Each method serves a specific purpose, whether it’s displaying information to the console, logging data to a file, or providing detailed debugging messages.

You may also like:

100 PowerShell cmdlets download free

100 POWERSHELL CMDLETS E-BOOK

FREE Download an eBook that contains 100 PowerShell cmdlets with complete script and examples.