locked
Trim away text in front and behind RRS feed

  • Question

  • Hi! I dont really know how to remove comma separated text, so I had to ask here...

    >rdp-tcp#3,Administrator,1,Active
    rdp-tcp#11,Test2,4,Active

    How can I remove the unnecessary text so that I only have left the user names Administrator and Test2?

    Not so experienced in Powershell yet, so I need a few tips


    Freddy

    Wednesday, March 30, 2016 11:47 AM

Answers

  • Hi,

    One method is to use .Split():

    $str1 = 'rdp-tcp#3,Administrator,1,Active'
    $pieces1 = $str1.Split(',')
    
    $str2 = 'rdp-tcp#11,Test2,4,Active'
    $pieces2 = $str2.Split(',')
    
    $pieces1[1]
    $pieces2[1]


    • Marked as answer by CesarTabares Wednesday, March 30, 2016 12:23 PM
    Wednesday, March 30, 2016 11:51 AM
  • Hi Cesar,

    Just as Mike post the split is a method, which you could use it to divide a string into an array. In your case, the split method divide "rdp-tcp#3,Administrator,1,Active" to 4 parts according to the ",". You can check it as below:

    As the Administrator is the second value in the array, then you could use $array[1] to output it, just as mike did on the above.

    Hope this would explain your confussion. If you have any other questions, feel free to post us.

    Best Regards,

    Elaine


    Please remember to mark the replies as answers if they help and unmark them if they provide no help. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    • Marked as answer by CesarTabares Thursday, March 31, 2016 7:37 AM
    Thursday, March 31, 2016 3:10 AM

All replies

  • Hi,

    One method is to use .Split():

    $str1 = 'rdp-tcp#3,Administrator,1,Active'
    $pieces1 = $str1.Split(',')
    
    $str2 = 'rdp-tcp#11,Test2,4,Active'
    $pieces2 = $str2.Split(',')
    
    $pieces1[1]
    $pieces2[1]


    • Marked as answer by CesarTabares Wednesday, March 30, 2016 12:23 PM
    Wednesday, March 30, 2016 11:51 AM
  • Hi Freddy,

    basically, PowerShell has two powerful tools for working on csv content:

    • Import-Csv (When reading from a csv file)
    • ConvertFrom-Csv (When reading text output from other commands)

    Try reading up on these cmdlets by running their help and reading their examples:

    Get-Help Import-Csv -Examples
    Get-Help ConvertFrom-Csv -Examples

    Cheers,
    Fred


    There's no place like 127.0.0.1

    Wednesday, March 30, 2016 12:01 PM
  • This seems to work as I wanted... A follow up question... What is -split? is it a parameter, or part of a cmdlet or what? Any link to documentation?

    Freddy

    Wednesday, March 30, 2016 12:10 PM
  • This seems to work as I wanted... A follow up question... What is -split? is it a parameter, or part of a cmdlet or what? Any link to documentation?

    Freddy

    It's a string method:

    http://ss64.com/ps/split.html


    Wednesday, March 30, 2016 12:12 PM
  • Hi Cesar,

    Just as Mike post the split is a method, which you could use it to divide a string into an array. In your case, the split method divide "rdp-tcp#3,Administrator,1,Active" to 4 parts according to the ",". You can check it as below:

    As the Administrator is the second value in the array, then you could use $array[1] to output it, just as mike did on the above.

    Hope this would explain your confussion. If you have any other questions, feel free to post us.

    Best Regards,

    Elaine


    Please remember to mark the replies as answers if they help and unmark them if they provide no help. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    • Marked as answer by CesarTabares Thursday, March 31, 2016 7:37 AM
    Thursday, March 31, 2016 3:10 AM