Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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

share|improve this question
    
possible duplicate of PowerShell: how to convert array object to string? –  Matt Nov 23 '14 at 21:23

1 Answer 1

up vote 3 down vote accepted

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 ', '
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.