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:
"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 ','