1

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
raj
  • 13
  • 1
  • 5
  • possible duplicate of [PowerShell: how to convert array object to string?](http://stackoverflow.com/questions/7723584/powershell-how-to-convert-array-object-to-string) – Matt Nov 23 '14 at 21:23

1 Answers1

4

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 ', '
Micky Balladelli
  • 9,781
  • 2
  • 33
  • 31