Write-Host "assoc.Id) - assoc.Name) - assoc.Owner)"

See the Windows PowerShell Language Specification Version 3.0, p25, sub-expressions expansion.

Answer from David Brabant on Stack Overflow
Discussions

Powershell Echo Statement + Variable In One line - Stack Overflow
I'm running a script, and I want it to print a "statement + variable + statement" at the end [when successful]. I've tried a few thing but it always returns as 3 separate lines, instead of one. Th... More on stackoverflow.com
🌐 stackoverflow.com
Formatting the output of a variable
I have a script which runs a PowerCLI command like this: Get-IScsiHbaTarget -IScsiHba $hba | Sort Address The output is like this: ========================================== iSCSI Targets on MyVMHost.mydomain.com Address Port Type 172.20.20.80 3260 Static 172.20.20.80 3260 Send 172.20.20.81 ... More on forums.powershell.org
🌐 forums.powershell.org
12
0
January 6, 2022
Display String Variable as Multiline but Single line output
Hello, I’m familiar with the Line Continuation in PowerShell feature for keeping code from running far out of view. Is there a way to use this method - or similar - to work with a long string variable? When I tried this, it would output the string as multiple lines instead of a single line. More on forums.powershell.org
🌐 forums.powershell.org
10
0
August 23, 2023
Capturing output to variable in console vs script
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." More on reddit.com
🌐 r/PowerShell
14
1
January 19, 2022
🌐
Red Gate Software
red-gate.com › home › working with powershell strings
Working with PowerShell strings | Simple Talk
November 24, 2021 - PowerShell starts with the object inside the parentheses (e.g. $person1.Name) and evaluates that, and then treats it as a variable denoted by the outer $. Write-host then evaluates that variable and inserts it into the string. Note this means you can do some additional fancy things such as the following ... The summation of the ages of $person1 and $person2 happens before Write-host evaluates the outer $. Imagine you are tasked with using PowerShell to print out invoices from an order management system.
🌐
EDUCBA
educba.com › home › data science › data science tutorials › powershell tutorial › powershell variable in string
PowerShell Variable in String | Examples of PowerShell Variable in String
March 13, 2023 - Similarly, for the other data types, the string can convert any variable to the string. ... $a = 10.2 Write-Output "Representing float $a to string" $b = "Hello World" Write-Output "Representing String $b to string" ... Single-quote and Double-Quote in the PowerShell are the same, but expanding the variable makes a huge difference because single-quote string can’t expand the variable.
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.
🌐
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 ...
Find elsewhere
🌐
Adam the Automator
adamtheautomator.com › powershell-variable-in-strings
Place PowerShell Variables in Strings: Quick Tips
October 1, 2023 - When you need to show a PowerShell variable in a string, you typically just add the variable along with some other text inside of a string with double-quotes as shown below
🌐
Powercmd
powercmd.com › home › powershell variables in strings: practical tips for using
PowerShell Variable in String: How to Master Variable Interpolation
October 4, 2023 - Subexpressions, enclosed within `$()`, empower you to execute complex expressions and include their outcomes in strings. For instance: ```powershell $quantity = 5 $pricePerUnit = 10 Write-Host "Total cost: $($quantity * $pricePerUnit)" ``` ... Consider scenarios where variables may be null or empty when interpolating them into strings.
🌐
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
🌐
Powershellbyexample
powershellbyexample.dev › post › strings
Strings | PowerShell By Example
In the sixth line, we assign the string to a variable using the PowerShell notation. This is also string. In the remaining lines we print out the variables in a couple different ways.
🌐
SS64
ss64.com › ps › write-output.html
Write-Output - PowerShell cmdlet
A variable, command or expression that gets the objects. ... Write-Output writes objects to the success pipeline. To have output appear only on screen use write-host or Write-Warning instead. If an unquoted string of characters is passed to write-output, they will be implicitly treated as a string, so write-output hello is equivalent to: write-output "hello" and write-output hello world is equivalent to: write-output "hello" "world" Note this is not the same as write-output "hello world"
🌐
ShellGeek
shellgeek.com › home › powershell › convert powershell variable to string
Convert PowerShell Variable to String - ShellGeek
April 14, 2024 - In PowerShell, you can convert the variable to a string using the ToString() method or the -f format operator. You can convert integer, bool,
🌐
ShellGeek
shellgeek.com › home › powershell › powershell echo equivalent command – write-output
PowerShell echo equivalent command - Write-Output - ShellGeek
April 14, 2024 - The PowerShell echo command is used to print strings or variables on the console. PowerShell echo alias command is Write-Output.
🌐
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 - The data type of this parameter is a string. The alias is msg. The default value is none. It accepts pipeline input, but wild card characters are not permitted. ... Write-Host "Example of verbose command" Write-Verbose -Message "Searching the error in Event viewer." Write-Verbose -Message "Searching the error in Event viewer." -Verbose ... Thus, the article covered in detail about printing in PowerShell.
Address   Unit no. 202, Jay Antariksh Bldg, Makwana Road, Marol, Andheri (East),, 400059, Mumbai
🌐
Powershellexplained
powershellexplained.com › 2017-01-13-powershell-variable-substitution-in-strings
Powershell: Everything you wanted to know about variable substitution in strings
January 13, 2017 - Especially so if there is just one value at the end of the string. But this can spiral get complicated quick. $first = 'Kevin' $last = 'Marquette' $message = 'Hello, ' + $first + ' ' + $last + '.' This is a very simple and common requirement and this is already getting harder to read. Powershell has another option that is very easy. You can specify your variables directly in the strings.
🌐
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
🌐
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