Powershell remove space from after command - powershell

I have this command
Write-Host "Background " -NoNewline; [System.Windows.Forms.SystemInformation]
::PrimaryMonitorSize.Width; Write-Host "x" -NoNewline;
[System.Windows.Forms.SystemInformation]::PrimaryMonitorSize.Height
I want it to come out Background 1920x1080
I cant seem to find a way to stop the command
[System.Windows.Forms.SystemInformation]::PrimaryMonitorSize.Width
from making a new line.
Its coming out
Background 1920
x1080

It's less complicated to simply use string formatting or in-line expand into a single string.
"Background {0}x{1}" -f [System.Windows.Forms.SystemInformation]::PrimaryMonitorSize.Width,[System.Windows.Forms.SystemInformation]::PrimaryMonitorSize.Height
or
Write-Host "Background $([System.Windows.Forms.SystemInformation]::PrimaryMonitorSize.Width)x$([System.Windows.Forms.SystemInformation]::PrimaryMonitorSize.Height)"
An article on various formatting options: https://blogs.technet.microsoft.com/heyscriptingguy/2013/03/12/use-powershell-to-format-strings-with-composite-formatting/
The reason you're getting the extra newline is that your code sample omitted a Write-Host on the Width portion. The first items went to Write-Host, then an item on the output stream that didn't have a way to omit the newline. Simply correcting that flaw gives you the output you desired, but the approach is overly complicated.
Fixed original sample:
Write-Host "Background " -NoNewline;
Write-Host ([System.Windows.Forms.SystemInformation]::PrimaryMonitorSize.Width) -NoNewLine;
Write-Host "x" -NoNewline;
Write-Host ([System.Windows.Forms.SystemInformation]::PrimaryMonitorSize.Height)

You are getting multiple lines because you are not calling Write-Host -NoNewLine on the command to output the width. Your code is running the following four commands
Write-Host "Background " -NoNewline
[System.Windows.Forms.SystemInformation]::PrimaryMonitorSize.Width
Write-Host "x" -NoNewline
[System.Windows.Forms.SystemInformation]::PrimaryMonitorSize.Height
The second and fourth commands insert a newline, because you didn't use Write-Host to tell them not to.
Write-Host is not usually the best way to output text. A better option would be to build the output string in one statement using PowerShell's -f formatting operator.
$width = [System.Windows.Forms.SystemInformation]::PrimaryMonitorSize.Width
$height = [System.Windows.Forms.SystemInformation]::PrimaryMonitorSize.Height
"Background {0}x{1}" -f ($width, $height)

Related

-NoNewLine Not working as expected in Powershell

I am using powershell to concatenate a key but the Writehost is giving me a carriage delimiter and 3 lines instead of a single line when using -NoNewLine.
I have tried -NoNewLine along with various other code sets such as
write-host -replace "[`r`n](-join(
Bearer
eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsImtpZCI6Ik5VTTBSRFE1UVROQk9FRTJNREJEUWtGR1JEazFNRGN4TlRReU9FUXpOalZFT0RZeVJEQXpSZyJ9.eyJpc3MiOiJodHRwczovL2lkZW50aXR5LmF1dGgwLWRldi5zcy5hd3MuYmNpZGFoby5jb20vIiwic3ViIjoidHFYQjEw
ejVUT0ZvZ05rNDAzQmZYZkhrTkJxSVJ0UENAY2xpZW50cyIsImF1ZCI6Imh0dHBzOi8vaWRlbnRpdHkuYXV0aDAtZGV2LnNzLmF3cy5iY2lkYWhvLmNvbS9hcGkvdjIvIiwiaWF0IjoxNTUzNTI2MDk0LCJleHAiOjE1NTM2MTI0OTQsImF6cCI6InRxWEIxMHo1VE9Gb2dOazQwM0JmWGZIa05C
cUlSdFBDIiwic2NvcGUiOiJyZWFkOmNsaWVudF9ncmFudHMgY3JlYXRlOmNsaWVudF9ncmFudHMgZGVsZXRlOmNsaWVudF9ncmFudHMgdXBkYXRlOmNsaWVudF9ncmFudHMgcmVhZDp1c2VycyB1cGRhdGU6dXNlcnMgZGVsZXRlOnVzZXJzIGNyZWF0ZTp1c2VycyByZWFkOnVzZXJzX2FwcF9t
PeIoyImIORI59k0Ee_Ay4jlSpiPT_FEdlln_ZWfcEDCPQ
I also have been scouring Stack overflow and everyone says having the no new line should be the answer
$assoc1 ="Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsImtpZCI6Ik5VTTBSRFE1UVROQk9FRTJNREJEUWtGR1JEazFNRGN4TlRReU9FUXpOalZFT0RZeVJEQXpSZyJ9.eyJpc3MiOiJodHRwczovL2lkZW50aXR5LmF1dGgwLWRldi5zcy5hd3MuYmNpZGFoby5jb20vIiwic3ViIjoidHFYQjEw"
$assoc2 ="ejVUT0ZvZ05rNDAzQmZYZkhrTkJxSVJ0UENAY2xpZW50cyIsImF1ZCI6Imh0dHBzOi8vaWRlbnRpdHkuYXV0aDAtZGV2LnNzLmF3cy5iY2lkYWhvLmNvbS9hcGkvdjIvIiwiaWF0IjoxNTUzNTI2MDk0LCJleHAiOjE1NTM2MTI0OTQsImF6cCI6InRxWEIxMHo1VE9Gb2dOazQwM0JmWGZIa05C"
$assoc3 ="cUlSdFBDIiwic2NvcGUiOiJyZWFkOmNsaWVudF9ncmFudHMgY3JlYXRlOmNsaWVudF9ncmFudHMgZGVsZXRlOmNsaWVudF9ncmFudHMgdXBkYXRlOmNsaWVudF9ncmFudHMgcmVhZDp1c2VycyB1cGRhdGU6dXNlcnMgZGVsZXRlOnVzZXJzIGNyZWF0ZTp1c2VycyByZWFkOnVzZXJzX2FwcF9t"
$apikey = ($assoc1,$assoc2,$assoc3)
write-host $apikey -NoNewLine
I would expect this to run and the result to be one line
"Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsImtpZCI6Ik5VTTBSRFE1UVROQk9FRTJNREJEUWtGR1JEazFNRGN4TlRReU9FUXpOalZFT0RZeVJEQXpSZyJ9.eyJpc3MiOiJodHRwczovL2lkZW50aXR5LmF1dGgwLWRldi5zcy5hd3MuYmNpZGFoby5jb20vIiwic3ViIjoidHFYQjEwejVUT0ZvZ05rNDAzQmZYZkhrTkJxSVJ0UENAY2xpZW50cyIsImF1ZCI6Imh0dHBzOi8vaWRlbnRpdHkuYXV0aDAtZGV2LnNzLmF3cy5iY2lkYWhvLmNvbS9hcGkvdjIvIiwiaWF0IjoxNTUzNTI2MDk0LCJleHAiOjE1NTM2MTI0OTQsImF6cCI6InRxWEIxMHo1VE9Gb2dOazQwM0JmWGZIa05CcUlSdFBDIiwic2NvcGUiOiJyZWFkOmNsaWVudF9ncmFudHMgY3JlYXRlOmNsaWVudF9ncmFudHMgZGVsZXRlOmNsaWVudF9ncmFudHMgdXBkYXRlOmNsaWVudF9ncmFudHMgcmVhZDp1c2VycyB1cGRhdGU6dXNlcnMgZGVsZXRlOnVzZXJzIGNyZWF0ZTp1c2VycyByZWFkOnVzZXJzX2FwcF9t"
instead the result set gives me 3 lines of codes instead of 1. Is this a limitation on powershell?
enter image description here
You can just join them, this way...
$assoc1,$assoc2,$assoc3 -join ''
Though this will show as 3 lines on screen, because of screen formatting, it is not.
($assoc1 ="Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsImtpZCI6Ik5VTTBSRFE1UVROQk9FRTJNREJEUWtGR1JEazFNRGN4TlRReU9FUXpOalZFT0RZeVJEQXpSZyJ9.eyJpc3MiOiJodHRwczovL2lkZW50aXR5LmF1dGgwLWRldi5zcy5hd3MuYmNpZGFoby5jb20vIiwic3ViIjoidHFYQjEw" ).Length
($assoc2 ="ejVUT0ZvZ05rNDAzQmZYZkhrTkJxSVJ0UENAY2xpZW50cyIsImF1ZCI6Imh0dHBzOi8vaWRlbnRpdHkuYXV0aDAtZGV2LnNzLmF3cy5iY2lkYWhvLmNvbS9hcGkvdjIvIiwiaWF0IjoxNTUzNTI2MDk0LCJleHAiOjE1NTM2MTI0OTQsImF6cCI6InRxWEIxMHo1VE9Gb2dOazQwM0JmWGZIa05C" ).Length
($assoc3 ="cUlSdFBDIiwic2NvcGUiOiJyZWFkOmNsaWVudF9ncmFudHMgY3JlYXRlOmNsaWVudF9ncmFudHMgZGVsZXRlOmNsaWVudF9ncmFudHMgdXBkYXRlOmNsaWVudF9ncmFudHMgcmVhZDp1c2VycyB1cGRhdGU6dXNlcnMgZGVsZXRlOnVzZXJzIGNyZWF0ZTp1c2VycyByZWFkOnVzZXJzX2FwcF9t").Length
($apikey = $assoc1,$assoc2,$assoc3 -join '').Length
# Results
220
220
220
660
$apikey.Count
1
$apikey | clip
If you paste this into notepad, without 'Word Wrap' on, you'll see it's one line.
Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsImtpZCI6Ik5VTTBSRFE1UVROQk9FRTJNREJEUWtGR1JEazFNRGN4TlRReU9FUXpOalZFT0RZeVJEQXpSZyJ9.eyJpc3MiOiJodHRwczovL2lkZW50aXR5LmF1dGgwLWRldi5zcy5hd3MuYmNpZGFoby5jb20vIiwic3ViIjoidHFYQjEwejVUT0ZvZ05rNDAzQmZYZkhrTkJxSVJ0UENAY2xpZW50cyIsImF1ZCI6Imh0dHBzOi8vaWRlbnRpdHkuYXV0aDAtZGV2LnNzLmF3cy5iY2lkYWhvLmNvbS9hcGkvdjIvIiwiaWF0IjoxNTUzNTI2MDk0LCJleHAiOjE1NTM2MTI0OTQsImF6cCI6InRxWEIxMHo1VE9Gb2dOazQwM0JmWGZIa05CcUlSdFBDIiwic2NvcGUiOiJyZWFkOmNsaWVudF9ncmFudHMgY3JlYXRlOmNsaWVudF9ncmFudHMgZGVsZXRlOmNsaWVudF9ncmFudHMgdXBkYXRlOmNsaWVudF9ncmFudHMgcmVhZDp1c2VycyB1cGRhdGU6dXNlcnMgZGVsZXRlOnVzZXJzIGNyZWF0ZTp1c2VycyByZWFkOnVzZXJzX2FwcF9t
If you want your 3 strings to be joined without separators, i.e., to be directly concatenated, use the unary form of the -join operator:
$apikey = -join $assoc1, $assoc2, $assoc3
If you want to output that single string without a trailing newline:
To print to the console only, you can use Write-Host -NoNewline $apikey
Note that as an artifact of display in the console your single-line string may still display across multiple lines, depending on the window / buffer width of your console window.
To save to a file, use Set-Content -NoNewline or Out-File -NoNewline (PSv5+).

Unnecessary space in output when using Write-Host

When I use Write-Host within a Foreach-Object, I get an unnecessary space in the output.
write-host "http://contoso.com/personal/"$_.ADUserName
Output:
http://contoso.com/personal/ john.doe
How can I remove the space before john? Trim does not work because there is no space in $_.ADUserName
This is happening because Write-Host is considering your constant string and your object to be two separate parameters -- you aren't actually joining the strings together the way you're calling it. Instead of calling it this way, actually concatenate the strings:
write-host "http://contoso.com/personal/$($_.ADUserName)"
or
write-host ("http://contoso.com/personal/" + $_.ADUserName)
or
write-host ("http://contoso.com/personal/{0}" -f $_.ADUserName)
Just do it without write-host:
"http://contoso.com/personal/{0}" -f $_.ADUserName

Muliple Foreground Colors in Powershell in One Command including spaces

I wanted to make a nice GUI for my little script. I've this post describing a function how to do it.
function Write-Color([String[]]$Text, [ConsoleColor[]]$Color) {
for ($i = 0; $i -lt $Text.Length; $i++) {
Write-Host $Text[$i] -Foreground $Color[$i] -NoNewLine
}
Write-Host
}
Write-Color -Text Red,White,Blue -Color Red,White,Blue
It generally works fine but it doesn't like when there are spaces. I wanted to make sure the output is good looking so I've added some spaces..
Write-Color -Text "`t`t`SQL Connectivity: ", "Verified ($gSerlServer\$gServerSqlDB)" -Color White, Green
Is there a way so that it would include the spaces and not act weirdly (at least from my point of view?). The funny thing is it sometimes display it correctly, and sometimes it doesn't. I don't have a clue why?
I've no idea why this works but adding Start-Sleep before executing colors solves the problem
Start-Sleep -Milliseconds 30
# Write-Color goes here...
So for every new display after clearing screen (going thru menus) I actually set the Start-Sleep. It's like execution is too fast with display before colors catch up the the screen.

Powershell Write-host outputting parameters in variable as string

$mycolorparams = "-foregroundcolor red -backgroundcolor black"
write-host "I want this foreground in Red and background in black" $mycolorparams
Hi All,
This is driving me nuts. When I use write-host the cmdlet returns everything as string:
"I want this foreground in Red and background in black -foregroundcolor red -backgroundcolor black".
Not the actual string with red text and a black background.
The worst part is this was working for me until I changed the var name in my code. I have no idea what has been altered since. I suspect it has something to do with the quotes as single quotes spit out the string and double quotes read the var. But after trying heaps of variations with single and double on both the text and var the results are the same just a string output.
I have been trawling the web for the past hour with no luck, plenty of examples but nothing I can find with the specific problem. Any help appreciated, thanks.
Use argument splatting (though I think it wasn't in old versions so you might need to upgrade to Powershell version 3 or later).
PS C:\> $opts = #{ForegroundColor="red"; BackgroundColor="black"; object="Hello world"}
PS C:\> write-host #opts
Hello world
or:
PS C:\> $opts = #{ForegroundColor="red"; BackgroundColor="black"}
PS C:\> write-host #opts -object "Hello world"
Hello world
You'll need to convert your options string into either a hashtable or an array. In fact if you run help about_Splatting you'll find one of the examples exactly covers your question:
This example shows how to re-use splatted values in different
commands.
The commands in this example use the Write-Host cmdlet to write messages
to the host program console. It uses splatting to specify the foreground
and background colors.
To change the colors of all commands, just change the value of the $Colors
variable.
The first command creates a hash table of parameter names and values and
stores the hash table in the $Colors variable.
$Colors = #{ForegroundColor = "black"
BackgroundColor = "white"}
The second and third commands use the $Colors variable for splatting in a
Write-Host command. To use the $Colors variable, replace the dollar sign
($Colors) with an At symbol (#Colors).
# Write a message with the colors in $Colors
Write-Host "This is a test." #Colors
# Write second message with same colors.
# The position of splatted hash table does not matter.
Write-Host #Colors "This is another test."
Hmm.. I have a little workaround for you in shape of a wrapper function:
$mycolorparams = " -foregroundcolor red -backgroundcolor black"
function redBlack($text){
invoke-expression ("write-host " + $text + $mycolorparams)
}
Then execute
redBlack "I want this foreground in Red and background in black"
which will yield a correctly coloured result.

How do I output text without a newline in PowerShell?

I want my PowerShell script to print something like this:
Enabling feature XYZ......Done
The script looks something like this:
Write-Output "Enabling feature XYZ......."
Enable-SPFeature...
Write-Output "Done"
But Write-Output always prints a new-line at the end so my output isn't on one line. Is there a way to do this?
Write-Host -NoNewline "Enabling feature XYZ......."
Unfortunately, as noted in several answers and comments, Write-Host can be dangerous and cannot be piped to other processes and Write-Output does not have the -NoNewline flag.
But those methods are the "*nix" ways to display progression, the "PowerShell" way to do that seems to be Write-Progress: it displays a bar at the top of the PowerShell window with progress information, available from PowerShell 3.0 onward, see manual for details.
# Total time to sleep
$start_sleep = 120
# Time to sleep between each notification
$sleep_iteration = 30
Write-Output ( "Sleeping {0} seconds ... " -f ($start_sleep) )
for ($i=1 ; $i -le ([int]$start_sleep/$sleep_iteration) ; $i++) {
Start-Sleep -Seconds $sleep_iteration
Write-Progress -CurrentOperation ("Sleep {0}s" -f ($start_sleep)) ( " {0}s ..." -f ($i*$sleep_iteration) )
}
Write-Progress -CurrentOperation ("Sleep {0}s" -f ($start_sleep)) -Completed "Done waiting for X to finish"
And to take the OP's example:
# For the file log
Write-Output "Enabling feature XYZ"
# For the operator
Write-Progress -CurrentOperation "EnablingFeatureXYZ" ( "Enabling feature XYZ ... " )
Enable-SPFeature...
# For the operator
Write-Progress -CurrentOperation "EnablingFeatureXYZ" ( "Enabling feature XYZ ... Done" )
# For the log file
Write-Output "Feature XYZ enabled"
While it may not work in your case (since you're providing informative output to the user), create a string that you can use to append output. When it's time to output it, just output the string.
Ignoring of course that this example is silly in your case but useful in concept:
$output = "Enabling feature XYZ......."
Enable-SPFeature...
$output += "Done"
Write-Output $output
Displays:
Enabling feature XYZ.......Done
To write to a file you can use a byte array. The following example creates an empty ZIP file, which you can add files to:
[Byte[]] $zipHeader = 80, 75, 5, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
[System.IO.File]::WriteAllBytes("C:\My.zip", $zipHeader)
Or use:
[Byte[]] $text = [System.Text.Encoding]::UTF8.getBytes("Enabling feature XYZ.......")
[System.IO.File]::WriteAllBytes("C:\My.zip", $text)
There seems to be no way to do this in PowerShell. All of the previous answers are not correct, because they do not behave the way Write-Output behaves but more like Write-Host which doesn't have this problem anyway.
The closes solution seems to use Write-Host with the -NoNewLine parameter. You can not pipe this which is a problem generally, but there is a way to override this function as described in Write-Host => Export to a file, so you can easily make it accept the parameter for an output file. This is still far from a good solution. With Start-Transcript this is more usable, but that cmdlet has problems with native applications.
Write-Outputsimply can't do what you need in a general context.
Yes, as other answers have states, it cannot be done with Write-Output. Where PowerShell fails, turn to .NET, there are even a couple of .NET answers here but they are more complex than they need to be.
Just use:
[Console]::Write("Enabling feature XYZ.......")
Enable-SPFeature...
Write-Output "Done"
It is not purest PowerShell, but it works.
The problem that I hit was that Write-Output actually linebreaks the output when using using PowerShell v2, at least to stdout. I was trying to write an XML text to stdout without success, because it would be hard wrapped at character 80.
The workaround was to use
[Console]::Out.Write($myVeryLongXMLTextBlobLine)
This was not an issue in PowerShell v3. Write-Output seems to be working properly there.
Depending on how the PowerShell script is invoked, you may need to use
[Console]::BufferWidth =< length of string, e.g. 10000)
before you write to stdout.
The answer by shufler is correct. Stated another way: Instead of passing the values to Write-Output using the ARRAY FORM,
Write-Output "Parameters are:" $Year $Month $Day
or the equivalent by multiple calls to Write-Output,
Write-Output "Parameters are:"
Write-Output $Year
Write-Output $Month
Write-Output $Day
Write-Output "Done."
concatenate your components into a STRING VARIABLE first:
$msg="Parameters are: $Year $Month $Day"
Write-Output $msg
This will prevent the intermediate CRLFs caused by calling Write-Output multiple times (or ARRAY FORM), but of course will not suppress the final CRLF of the Write-Output commandlet. For that, you will have to write your own commandlet, use one of the other convoluted workarounds listed here, or wait until Microsoft decides to support the -NoNewline option for Write-Output.
Your desire to provide a textual progress meter to the console (i.e. "....") as opposed to writing to a log file, should also be satisfied by using Write-Host. You can accomplish both by collecting the msg text into a variable for writing to the log AND using Write-Host to provide progress to the console. This functionality can be combined into your own commandlet for greatest code reuse.
I cheated, but I believe this is the only answer that addresses every requirement. Namely, this avoids the trailing CRLF, provides a place for the other operation to complete in the meantime, and properly redirects to stdout as necessary.
$c_sharp_source = #"
using System;
namespace StackOverflow
{
public class ConsoleOut
{
public static void Main(string[] args)
{
Console.Write(args[0]);
}
}
}
"#
$compiler_parameters = New-Object System.CodeDom.Compiler.CompilerParameters
$compiler_parameters.GenerateExecutable = $true
$compiler_parameters.OutputAssembly = "consoleout.exe"
Add-Type -TypeDefinition $c_sharp_source -Language CSharp -CompilerParameters $compiler_parameters
.\consoleout.exe "Enabling feature XYZ......."
Write-Output 'Done.'
A simplification to FrinkTheBrave's response:
[System.IO.File]::WriteAllText("c:\temp\myFile.txt", $myContent)
You simply cannot get PowerShell to omit those pesky newlines... There is no script or cmdlet that does. Of course, Write-Host is absolute nonsense, because you can't redirect/pipe from it!
Nevertheless, you can write your own EXE file to do it which is what I explained how to do in Stack Overflow question How to output something in PowerShell.
Write-Host is terrible, a destroyer of worlds, yet you can use it just to display progress to a user whilst using Write-Output to log (not that the OP asked for logging).
Write-Output "Enabling feature XYZ" | Out-File "log.txt" # Pipe to log file
Write-Host -NoNewLine "Enabling feature XYZ......."
$result = Enable-SPFeature
$result | Out-File "log.txt"
# You could try{}catch{} an exception on Enable-SPFeature depending on what it's doing
if ($result -ne $null) {
Write-Host "complete"
} else {
Write-Host "failed"
}
$host.UI.Write('Enabling feature XYZ.......')
Enable-SPFeature...
$host.UI.WriteLine('Done')
I'm not an expert by any means, but why not this:
Write-Output "hello" | ForEach-Object { $PSItem.Trim() } | Do-Whatever
This maintains the pipeline semantics but just trims the new line characters before passing it on down the pipeline to whatever you need to do next. If that is writing to a file, so be it. If that is writing to the host, you can do that, but if you do write it to the host, remember to use | Write-Host -NoNewline
UPDATE: As per my comment below: "I see why my answer won't work.. Powershell inevitable appends a new line char as part of it's piping semantics when piping to external programs. See this: github.com/PowerShell/PowerShell/issues/5974 Therefore when I pass the trimmed text down the pipeline, the new line char will re-appear in the input."
The following will place the cursor back at beginning of the previous row. It's up to you to place it in the right horizontal position (using $pos.X to move it sideways):
$pos = $host.ui.RawUI.get_cursorPosition()
$pos.Y -= 1
$host.UI.RawUI.set_cursorPosition($Pos)
Your current output is 27 spaces over, so $pos.X = 27 might work.
It may not be terribly elegant, but it does exactly what OP requested. Note that the ISE messes with StdOut, so there will be no output. In order to see this script work it can't be run within the ISE.
$stdout=[System.Console]::OpenStandardOutput()
$strOutput="Enabling feature XYZ... "
$stdout.Write(([System.Text.Encoding]::ASCII.GetBytes($strOutput)),0,$strOutput.Length)
Enable-SPFeature...
$strOutput="Done"
$stdout.Write(([System.Text.Encoding]::ASCII.GetBytes($strOutput)),0,$strOutput.Length)
$stdout.Close()
Simplest way with in-line concatenation - and while moving across to 'Write-Output' instead; e.g. 2 tab characters (string) & then a literal/verbatim (string):
Write-Output ("`t`t" + '${devArg}')
desired o/p:
Enabling feature XYZ......Done
you can use below command
$a = "Enabling feature XYZ"
Write-output "$a......Done"
you have to add variable and statement inside quotes. hope this is helpful :)
Thanks
Techiegal
From the link in the comments, that answer includes:
Write-Output "Some text $( $var.Name )"
which worked very well for me. The $( ) is not redundant if you need to ExpandProperty to get the individual value of Name, otherwise my output was this instead of the resolved value:
#{Name=Name; Address=Address; City=City}.Name
You can absolutely do this. Write-Output has a flag called "NoEnumerate" that is essentially the same thing.