From $Window.FindName("Server_list").SelectedItems
(note that I've removed what I presume to be an extraneous (
) I infer that you're getting a WPF list view's .SelectedItems
property value.
This value is a collection (strictly speaking: an object that implements the IList
interface)[1], which has two implications in PowerShell:
You can enumerate the collection (iterate over its elements) in a foreach
loop or, implicitly, in the pipeline (see bottom section for a ForEach-Object
example). Additionally, many PowerShell operators either operate exclusively on or can also operate on collections (typically, on the LHS), in which case the collection is also implicitly enumerated and the operation performed on each element, such as shown with the -join
operator below.
When you print the collection to the console (host), PowerShell's default output formatting system prints each element on its own line by default (see bottom section).
It sounds like you truly need to build a multi-line string programmatically:
To create a plain-text multi-line string, Mathias R. Jessen's solution based on the -join
operator is the right one:
# Note: Since -join is a *string* operator, any LHS elements
# that aren't already strings are automatically stringified with .ToString()
$Servers -join "`n"
Note that I'm using "`n"
, a Unix-format LF newline, which is usually sufficient. If you want to use Windows-format CRLF newlines, use "`r`n"
. For the platform-native newline sequence, use [Environment]::NewLine
.
However, if the text is to be rendered as HTML, actual newlines aren't sufficient: they get converted to spaces. Instead, you must join the server names with <br>
elements:
# Create a multi-line *HTML* string
$Servers -join '<br>'
Console-output considerations:
The following array-based example demonstrates the default to-display behavior with string collections (arrays):
PS> $Servers = 'server1', 'server2', 'server3'; $Servers # output
server1
server2
server3
Note that $Servers
by itself is essentially the same as Write-Output $Servers
, thanks to PowerShell's implicit output behavior.
If the elements of collection $Servers
aren't actually strings, you'll get different, richer output: depending on the number of properties of the objects, you'll get table-formatted (<= 4 properties) or list-formatted (5 or more properties, each property on its own line) output by default.
If you just want the string representations of these elements, as reported by their .ToString()
method, use the following:
$Servers | ForEach-Object ToString
By contrast, using Write-Host
- which is meant for to-display output, not for data output - stringifies collections differently (and often unhelpfully); it outputs a single line, with the (stringified) collection elements joined with spaces, and uses simple .ToString()
stringification, not PowerShell's rich output-formatting system.
PS> Write-Host $Servers
server1 server2 server3
However, you can instruct Write-Host
to use newlines (line breaks) instead:
PS> Write-Host -Separator "`n" $Servers
server1
server2
server3
However, remember that this generates for-display output, not data (see below).
[1] For a summary of which types PowerShell enumerates by default, see the bottom section of this answer.