There are several ways:

Write-Host: Write directly to the console, not included in function/cmdlet output. Allows foreground and background colour to be set.

Write-Debug: Write directly to the console, if $DebugPreference set to Continue or Stop.

Write-Verbose: Write directly to the console, if $VerbosePreference set to Continue or Stop.

The latter is intended for extra optional information, Write-Debug for debugging (so would seem to fit in this case).

Additional: In PSH2 (at least) scripts using cmdlet binding will automatically get the -Verbose and -Debug switch parameters, locally enabling Write-Verbose and Write-Debug (i.e. overriding the preference variables) as compiled cmdlets and providers do.

Answer from Richard on Stack Overflow
🌐
EITCA Academy
eitca.org › home › how can you output the value stored in a variable in powershell?
How can you output the value stored in a variable in PowerShell? - EITCA Academy
August 5, 2023 - To output the value stored in a variable in PowerShell, you can use the Write-Output cmdlet or simply type the name of the variable. Both methods allow you to display the value on the console or redirect it to other commands or files.
Discussions

How to format powershell command output to a variable without the property?
I haven’t done any scripting for a long while and certainly not in powershell. How do I format powershell command output to a variable without the property? For example, when I run the following command it puts the property in the variable when I just want the actual serial number object. More on community.spiceworks.com
🌐 community.spiceworks.com
9
9
February 16, 2020
How to print environment variables to the console in PowerShell? - Stack Overflow
I'm starting to use PowerShell and am trying to figure out how to echo a system environment variable to the console to read it. Neither of the below are working. The first just prints %PATH%, and the second prints nothing. ... For example, if you want to print the value of environment value MINISHIFT_USERNAME, then command ... More on stackoverflow.com
🌐 stackoverflow.com
Variables for Input(s) and output
Hello, Pretty new to powershell, (at least on the learning how to script front) and have recently started to dig in and figure out how to do everything. I have some good scripts from a previous co-worker, and after 24 hours of Powershell youtube videos, I can understand everything they do now! More on forums.powershell.org
🌐 forums.powershell.org
0
0
February 19, 2022
Outputting variable value
Hi everyone, I am new to PowerShell so apologies if I don’t explain this clearly. I have a file that contains a list of directories/folders in a CSV. I want to loop over each of those file paths and return the users who have access to that file directory/folders. More on forums.powershell.org
🌐 forums.powershell.org
4
0
October 3, 2022
🌐
Reddit
reddit.com › r/powershell › capturing output to variable in console vs script
r/PowerShell on Reddit: Capturing output to variable in console vs script
January 19, 2022 -

Executing the following in a PS 7.2 console:

$result=$(Get-AzResourceGroup -Name "rgname1")
$result

Results in the following output:

ResourceGroupName : rgname1
Location          : eastus
ProvisioningState : Succeeded
Tags              :
ResourceId        : /subscriptions/redacted/resourceGroups/rgname1

However, executing a script containing the following:

[...]
$result=$(Get-AzResourceGroup -Name "rgname1")
Write-Output (Additional info here: $result)
[...]

Results in the following output:

Additional info here: Microsoft.Azure.Commands.ResourceManager.Cmdlets.SdkModels.PSResourceGroup

The script output is printing the object type but not the actual contents. I'm attempting to capture the full cmdlet output to a variable, to eventually write it to a file or elsewhere. My Google foo is failing I guess, but I've seen multiple examples of the variable = <cmdlet> variety that suggest this should work. Thanks for any feedback.

Edit: I realize now that var = <cmdlet> behavior is the same in the console and in a script. My question is why does Write-Output output the object type as opposed to the contents of the variable.

Top answer
1 of 3
3
PowerShell automatically adds a hidden Out-Default to the end of every pipeline so it is what you normally see as a displayed result. Out-Default looks up a table linking object type to format type (which can be customised.) Out-Default is not useful to grab the results because it sends the conversion straight to output (normally the console). When you use a string substitution like "value : $x" you get the $x.ToString() method. This is the same as Out-Default for simple objects that convert to (or are) a string, eg, a number. For objects that are too complex to convert to a simple string you get the object type, which isn't that useful. It's basically saying format this yourself, buddy. If you want a list format or whatever saved in a string variable you can do that, but it's a a bit messy. Use this construction: $var| Format-List | Out-String. Try these: $s = 'My string.' $s $s | Out-Default $s.ToString() '---' $h = @{ animal = 'Dog' } # hash table $h $h.ToString() "123 $h 456." $x = $h | Format-List | Out-String $x "abc $x xyz."
2 of 3
3
I have no Azure Resource groups to test it myself but I assume you get such output because Write-Output tries to assume this is a single string you want to output. I assume the problem you are facing is powershell tries to convert your $result variable into a string by using a .ToString() methond on it. This text you're getting is probably the result of that. By default the method is missing on an object it output's it's type. Since what you want to do is to output a string header Additional info here: and then the value of $result I assume you should write something along the lines of the following: # Default formatting Write-Output -InputObject "Additional info here:", $result # Format as list Write-Output -InputObject "Additional info here:", $result | Format-List # Format as table Write-Output -InputObject "Additional info here:", $result | Format-Table That way Write-Output outputs a two element array with string as the first element and your $result as the seccond. Notice that in the last two examples I don't use brackets and it still assumes the pipeline is just for the $result. It acts this way because Write-Output by it's definition expects a single psobject as an argument for -InputObject therefore this is equivalent to the following code: $Out = @( "Additional info here:", $result | Format-Table ) Write-Output -InputObject $Out
🌐
PowerShell Test-Path
powershellfaqs.com › powershell-print-variable
PowerShell Print Variable [With Examples]
February 14, 2025 - 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 ...
🌐
EDUCBA
educba.com › home › data science › data science tutorials › powershell tutorial › powershell print
PowerShell print | Different Ways of Printing Output in PowerShell
March 6, 2023 - Write-Host "Demo of write-debug" Write-Debug "This wont be printed" Write-Debug "my name is vignesh" Write-Host "Changing the value of debug preference variable" -ForegroundColor Green Write-Host "Current value is" -ForegroundColor Green $DebugPreference Write-Debug "wont print this on console" $DebugPreference = "Continue" Write-Debug "now this will be displayed" ... The Write-Verbose cmdlet writes text to the verbose message stream in PowerShell. Typically, the verbose message stream is employed to deliver more thorough information about command processing.
Address   Unit no. 202, Jay Antariksh Bldg, Makwana Road, Marol, Andheri (East),, 400059, Mumbai
🌐
Microsoft Learn
learn.microsoft.com › en-us › powershell › module › microsoft.powershell.utility › write-output
Write-Output (Microsoft.PowerShell.Utility) - PowerShell | Microsoft Learn
In this example, the results of the Get-Process cmdlet are stored in the $P variable. The Write-Output cmdlet displays the process objects in $P to the console. ... This command pipes the "test output" string to the Get-Member cmdlet, which displays the members of the System.String class, demonstrating that the string was passed along the pipeline.
🌐
Delft Stack
delftstack.com › home › howto › powershell › print environment variables in powershell
How to Print Environment Variables in Windows PowerShell | Delft Stack
February 12, 2024 - This command is akin to taking an inventory of the settings and configurations that the operating system and applications use. By executing Get-ChildItem Env:, we are presented with a comprehensive list, including each variable’s name and value. ... The flexibility of PowerShell allows us to combine Get-ChildItem with other commands using piping.
Find elsewhere
🌐
Powershell Commands
powershellcommands.com › print-variable-powershell
Print Variable PowerShell: A Quick How-To Guide
July 8, 2024 - The variable `$myVariable` passes its content through `Write-Output`, demonstrating how easily you can print variable values. ... In PowerShell, `echo` is an alias for `Write-Output`. This means using `echo` achieves the same result as invoking `Write-Output`. ... Similar to `Write-Output`, this will print "Welcome to PowerShell!" to the console. In PowerShell, using aliases can increase your efficiency, as they often require less typing. ... You can also pipe `echo` output to another command.
Top answer
1 of 9
9

I haven’t done any scripting for a long while and certainly not in powershell.

How do I format powershell command output to a variable without the property? For example, when I run the following command it puts the property in the variable when I just want the actual serial number object.

$SerialNumber = (get-wmiobject -Class win32_bios | select SerialNumber)

Output for the variable $SerialNumber is everything in quotes:

"SerialNumber

ZMMJKKYTT"

I just want “ZMMJKKYTT”, not the SerialNumber or the --------.

I’m trying to write a basic script to change computer name and join to domain. We have a basic naming convention of “ABCD-SerialNumber”.

I’ve found scripts that will do the computer name change and the domain join and a way to gather the serial number. I can’t for the life of me figure out how to get the output for the serial number into our naming convention.

Once I figure out the answer to the above question, I want to then change the computer name to “ABCD-$SerialNumber”. IE, ABCD-ZMMJKKYTT.

Once I figure this out I could just modify the script in this post: (Script to rename computer, then join it to domain. - Programming & Development - Spiceworks Community)

$hostname = read-host 'hostname'
$Domain = 'domain.com' ## put domain name here
$Credential = Get-Credential

Rename-Computer $hostname
Add-Computer -Domain $Domain -NewName $hostname -Credential $Credential -Restart -Force

In case I wasn’t clear, I want to:

Get the serial number of a local pc via powershell

Put only the serial number into a variable named $SerialNumber

Create a new variable called $NewCompName that is formatted as “ABCD-$SerialNumber”

Once I have $NewCompName formatted correctly, I can use one of the many other scripts to change the computer name and join to the domain such as:

$hostname = read-host ‘hostname’
$Domain = ‘domain.com’ ## put domain name here
$Credential = Get-Credential

Rename-Computer $NewCompName
Add-Computer -Domain $Domain -NewName $NewCompName -Credential $Credential -Restart -Force

Any help is greatly appreciated. I thought this would be easy but I don’t have hours to spend figuring it out. Thanks

2 of 9
2

Don’t use select and call variable.serialnumber

$var = Get-WmiObject Win32_BIOS -Property SerialNumber

$var.SerialNumber
🌐
PhoenixNAP
phoenixnap.com › home › kb › sysadmin › how to use echo in powershell
How to Use Echo in PowerShell
December 11, 2025 - For instance, print a line This sentence is white with a magenta background: Write-Host "This sentence is white with a magenta background" -ForegroundColor White -BackgroundColor Magenta · The output is displayed in the requested format. ... After reading this article, you know how to use echo, Write-Output, and Write-Host in PowerShell. Next, learn how to set environment variables in Windows.
Top answer
1 of 5
705

Prefix the variable name with env:

$env:path

For example, if you want to print the value of environment value MINISHIFT_USERNAME, then command will be:

$env:MINISHIFT_USERNAME

In case the environment variable label contains characters otherwise interpreted as bareword token terminators (like . or - or ), qualify the variable path expression with {...}:

${env:MINISHIFT-USERNAME}

You can also enumerate all variables via the env drive:

Get-ChildItem env:
2 of 5
24

The following works best, in my opinion:

Get-Item Env:PATH
  1. It's shorter and, therefore, a little easier to remember than Get-ChildItem (There's no hierarchy with environment variables).
  2. The command is symmetrical to one of the ways being used for setting environment variables with Powershell. (EX: Set-Item -Path env:SomeVariable -Value "Some Value")
  3. If you get in the habit of doing it this way, you'll remember how to list all Environment variables: simply omit the entry portion. (EX: Get-Item Env:)

I found the syntax odd at first, but things started making more sense after I understood the notion of Providers. Essentially PowerShell lets you navigate disparate components of the system in a way that's analogous to a file system.

What's the point of the trailing colon in Env: ? Try listing all of the "drives" available through Providers like this:

PS> Get-PSDrive

I only see a few results (Alias, C, Cert, D, Env, Function, HKCU, HKLM, Variable, WSMan). It becomes obvious that Env is simply a specific "drive", and the colon is the familiar syntax to anyone who's worked with Windows.

You can traverse through the drives like this:

Get-ChildItem C:\Windows
Get-Item C:
Get-Item Env:
Get-Item HKLM:
Get-ChildItem HKLM:SYSTEM
🌐
Microsoft Learn
learn.microsoft.com › en-us › powershell › module › microsoft.powershell.core › about › about_variables
about_Variables - PowerShell | Microsoft Learn
January 2, 2025 - To get a list of all the variables in your PowerShell session, type Get-Variable. The variable names are displayed without the preceding dollar ($) sign that is used to reference variables.
🌐
ShellGeek
shellgeek.com › home › powershell › powershell print environment variables
PowerShell Print Environment Variables - ShellGeek
April 14, 2024 - In this tutorial, I will explain how to print environment variables or echo environment variables using PowerShell. PowerShell Tip: dir, gci and ls are the PowerShell Get-ChildItem aliases to show environment variables. PowerShell – Get-ChildItem alias to echo environment variables ... Let’s check each one of the PowerShell commands to echo environment variables with examples.
🌐
PowerShell Forums
forums.powershell.org › powershell help
Variables for Input(s) and output - PowerShell Help - PowerShell Forums
February 19, 2022 - Hello, Pretty new to powershell, (at least on the learning how to script front) and have recently started to dig in and figure out how to do everything. I have some good scripts from a previous co-worker, and after 24 …
🌐
O'Reilly
oreilly.com › library › view › windows-powershell-quick › 0596528132 › ch01s20.html
Capturing Output - Windows PowerShell Quick Reference [Book]
September 27, 2006 - Capturing OutputThere are several ways to capture the output of commands in PowerShell:$variable = <Command>Stores the objects produced by the PowerShell command into... - Selection from Windows PowerShell Quick Reference [Book]
Author   Lee Holmes
Published   2006
Pages   115
🌐
PowerShell Forums
forums.powershell.org › powershell help
Outputting variable value - PowerShell Help - PowerShell Forums
October 3, 2022 - Hi everyone, I am new to PowerShell so apologies if I don’t explain this clearly. I have a file that contains a list of directories/folders in a CSV. I want to loop over each of those file paths and return the users who…
🌐
LazyAdmin
lazyadmin.nl › home › how to use powershell write output
How to use PowerShell Write Output — LazyAdmin
February 29, 2024 - Use the Write-Output cmdlet in PowerShell to print results or echo the value of variables. Including difference between Write-Host and Write-Output
🌐
TestMu AI Community
community.testmuai.com › ask a question
How can I display variables and their values in a PowerShell script, similar to using echo in PHP? - TestMu AI Community
August 31, 2024 - How to Output Variables and Values in a PowerShell Script? I would like to output variables and values in a PowerShell script by setting up flags and seeing the data matriculate throughout the script. How would I do thi…