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
🌐
Microsoft Learn
learn.microsoft.com › en-us › powershell › module › microsoft.powershell.utility › write-output
Write-Output (Microsoft.PowerShell.Utility) - PowerShell | Microsoft Learn
Specifies the objects to send down the pipeline. Enter a variable that contains the objects, or type a command or expression that gets the objects.
Discussions

How to Get formatted Output PowerShell command in to a variable
Hi, I am new here. I am working on some powershell script which I am using to print some formatted tables but I am keep getting: Microsoft.PowerShell.Commands.Internal.Format.GroupStartData Microsoft.PowerShell.Command… More on community.spiceworks.com
🌐 community.spiceworks.com
4
4
October 20, 2021
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
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
Making powershell output into a variable
New to Powershell. Been stumped on this for a while. I’m trying to get the following command to output the results, Caption and Portname, into two separate variables. Get-WmiObject win32_printer -ComputerName SERVER | select Caption,Portname I’ve tried a few different commands but none ... More on community.spiceworks.com
🌐 community.spiceworks.com
4
4
February 8, 2019
🌐
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 - In this syntax, `$VariableName` represents the name of the variable that contains the value you want to output. For example, if you have a variable named `$UserName` that stores a user's name, you can display it using the Write-Output cmdlet like this: powershell $UserName = "John Doe" Write-Output -InputObject $UserName
🌐
Harness Developer Hub
developer.harness.io › continuous delivery & gitops › knowledge base article › output variable for powershell script
Output variable for Powershell Script | Harness Developer Hub
October 10, 2025 - The Output Variables is used to export variables from the script to other steps in the stage. In the Powershell script you need to use the $env with the variable name.
🌐
Microsoft Learn
learn.microsoft.com › en-us › powershell › module › microsoft.powershell.utility › out-printer
Out-Printer (Microsoft.PowerShell.Utility) - PowerShell | Microsoft Learn
$H = Get-Help -Full Get-CimInstance Out-Printer -InputObject $H · Get-Help gets the full version of the Help topic for Get-CimInstance and stores it in the $H variable.
Find elsewhere
🌐
PowerShell Test-Path
powershellfaqs.com › output-variables-in-powershell
Output Variables in PowerShell
September 12, 2024 - It is particularly useful when you want to output variable values for debugging purposes without displaying them to the end user. ... 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. ... The Write-Verbose cmdlet in PowerShell is used to display verbose messages, which provide additional details about the script’s operation.
🌐
Microsoft Learn
learn.microsoft.com › en-us › powershell › scripting › lang-spec › chapter-04
Types - PowerShell | Microsoft Learn
In PowerShell, this type is System.Management.Automation.PSCustomObject. The cmdlets Import-Module and New-Object can generate an object of this type. The automatic variable $PSCmdlet is an object that represents the cmdlet or function being executed.
🌐
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…
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
🌐
Microsoft Learn
learn.microsoft.com › en-us › powershell › module › microsoft.powershell.core › out-null
Out-Null (Microsoft.PowerShell.Core) - PowerShell | Microsoft Learn
The cmdlets that contain the Out verb (the Out cmdlets) do not have parameters for names or file paths. To send data to an Out cmdlet, use a pipeline operator (|) to send the output of a PowerShell command to the cmdlet. You can also store data in a variable and use the InputObject parameter to pass the data to the cmdlet.
🌐
PowerShell Forums
forums.powershell.org › powershell help
Print the Cmdlet Output to a log file as well as display in the Console - PowerShell Help - PowerShell Forums
March 4, 2022 - I need to Print the Cmdlet Output to the Console and then I wanted the same to be send to it a log file as well. Like Start-Transcript cmdlet. Is there any way that I can achieve this.
🌐
LazyAdmin
lazyadmin.nl › home › how to use powershell variables
How to use PowerShell Variables — LazyAdmin
January 18, 2024 - To print a variable in PowerShell you can just type the variable name (with the $ sign) in the console to output the value.
🌐
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 ...
Top answer
1 of 16
1

Yes that is what Write-Output does. It writes output directly to the Output stream, which other parts of the script can pick up on. It is somewhat synonymous with return in this regard.

Depending on what your requirements are there are a number of different streams you could write to.

Write-Error

Write-Warning

Write-Verbose (as you have already picked up on)

Write-Debug

Write-Information

Write-Host (Gets puppies killed though Write-Host Considered Harmful | Jeffrey Snover's blog )

So maybe Write-Information is a better fit. It works in a very similar way to Write-Host except without the stupid colours that people insist on using and you can redirect it off elsewhere if you personally don’t want to read it (I rarely do and for me verbose is usually much more useful if something is going wrong anyway).

2 of 16
3

I’ve narrowed down a problem I’ve asked about before here so hopefully this will bring about the best answer. My issue is that I need to use Write-Output in many places throughout my script however it will not always work. This occurs when I attempt to use Write-Output in a function that returns a value. So, here are two examples.

This would work:

function hello{
	for ($i=0;$i-lt5;$i++){
		Write-Output "Hello World $i"
	}
}
hello

#Output

Hello World 0
Hello World 1
Hello World 2
Hello World 3
Hello World 4

This would NOT work;

function getOut ($number){
	if ($number -eq 1){
		Write-Output "Uh oh"
		$retVal = "false"
	}
	else{
		Write-Output Woo
		$retVal = "true"
	}
	return $retVal
}
function hello{
	$boolQ = 0
	$outPut = getOut $boolQ
	Write-Output "Finished!"
}
hello

#Output

Finished!

Now, I’m still unsure to the exact cause but from testing, it’s clear that functions that are called in this sort of style:

$val = myFunction

don’t seem to like Write-Output. Now I guess the reason is because the pipe is full with $val or something and I imagine many will suggest Write-Verbose but I honestly am just not a fan of the "Verbose: " part. I’m also writing this to a log using Add-Content (I find Out-File a bit more clunky and I don’t need to read and write at the same time so I’m happy using Add-Content).

I would be happy using verbose however I really need the "Verbose: " bit gone.

🌐
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