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

1

1 Answer 1

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 ', '
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.