Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can I put my Invoke-Command results in a comma-delimited string

PowerShell 5.1

How can I get the results of Invoke-Command into a comma-delimited string

Invoke-Command -ComputerName $manyComputers -Credential $credential -ScriptBlock {
   hostname
}  

expected output:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

"server1,server2"

>Solution :

Use -join in Windows PowerShell 5.1:

$result = (Invoke-Command -ComputerName $manyComputers -Credential $credential -ScriptBlock {
    $env:COMPUTERNAME
}) -join ','

In PowerShell 7+ you can use Join-String, much nicer syntax:

$result = Invoke-Command -ComputerName $manyComputers -Credential $credential -ScriptBlock {
    $env:COMPUTERNAME
} | Join-String -Separator ','
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading