locked
Changing break line with commas RRS feed

  • Question

  • So i tried using -replace to remove the break lines and replace with commas, but it didn't work.

    This is the code:

    foreach ($service in $checkservice){$service.Name -replace '\n\r',','}

    Output

    CIM Server

    CIM Secure Server

    CIM SLP

    DHCP

    NFC

    ...

    What I want is

    CIM Server, CIM Secure Server, CIM SLP, DHCP, ....

    Is there other ways that can be done?

    Sunday, July 23, 2017 7:23 AM

Answers

  • This has no breaks: "foreach ($service in $checkservice){$service.Name -replace '\n\r',','}"

    $names = foreach ($service in $checkservice){$service.Name }

    $names is now an array of names.

    $names -join ','

    $names is now a comma separated string of names.

    An array does not have line breaks it is just displayed that way.

    This is why you need to learn PowerShell and basic computer programming before you can use it.


    \_(ツ)_/

    • Marked as answer by BobbyTan Sunday, July 23, 2017 1:19 PM
    Sunday, July 23, 2017 12:03 PM

All replies

  • $checkservice.Name -join ', '
    Sunday, July 23, 2017 9:34 AM
  • It doesn't work. It still listing the names in the output.
    Sunday, July 23, 2017 10:30 AM
  • It might be helpful to see your code .... and to know what you actually want to achieve.

    Perhaps one of the simplest solution could be this:

    (Get-Service).Name -join ','


    Grüße - Best regards

    PS:> (79,108,97,102|%{[char]$_})-join''




    • Edited by BOfH-666 Sunday, July 23, 2017 11:34 AM
    Sunday, July 23, 2017 11:24 AM
  • This has no breaks: "foreach ($service in $checkservice){$service.Name -replace '\n\r',','}"

    $names = foreach ($service in $checkservice){$service.Name }

    $names is now an array of names.

    $names -join ','

    $names is now a comma separated string of names.

    An array does not have line breaks it is just displayed that way.

    This is why you need to learn PowerShell and basic computer programming before you can use it.


    \_(ツ)_/

    • Marked as answer by BobbyTan Sunday, July 23, 2017 1:19 PM
    Sunday, July 23, 2017 12:03 PM