I have a variable with the following output in PowerShell. How do I join the result to a string?
For example
$s
Output:
Name
abc
def
ghi
I want the output to be in a single string as below:
Name
abc, def, ghi
I have a variable with the following output in PowerShell. How do I join the result to a string?
$s
Output:
abc
def
ghi
I want the output to be in a single string as below:
abc, def, ghi
You can join $s using a comma. Try this:
$s -join ','
Edit, I noticed you want a space after the comma, so:
$s -join ', '
Output is this:
abc, def, ghi
Edit, if you want to save the result in $s do the following:
$s = $s -join ', '
New Edit. I didn't see your code, now it's clearer. Try the following:
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.RMO") | Out-Null
$repsvr = New-Object "Microsoft.SqlServer.Replication.ReplicationServer" "localhost"
$s = ($repsvr | select -expand RegisteredSubscribers | select Name) -join ', '