none
store only part of a string that follows a character / RRS feed

  • Question

  • Hi I'm hoping this will be a simple one for someone.

    I am using a line of code to capture a users ID which works perfect most of the time - until today where I encountered an issue.

    I use the following line of code  

    (((Get-WmiObject Win32_ComputerSystem).UserName).substring(13))

    which turns this  "TESTDOMAIN01\1234567" into this "1234567"

    all as expected.

    Today I encountered a problem when I tested on another domain which was "TESTDOM01\1234567"  I got back "34567"

    Which is completely right.

    So my question is  - is there a way to say take this string 

    ((Get-WmiObject Win32_ComputerSystem).UserName)

    output = "TESTDOM01\1234567"

    But display everything after the "\"

    If I can do this it wouldn't matter the length of the sting I would always capture everything after the "\"

    P.S I know there are other ways to capture a users ID but i'm interested in this method and if there is a solution it may come in handy in future for other string filtering, so I am not looking for other ways to capture the ID. 

    Also I am using PowerShell v2

    Thanks in advance

    Joe


    • Edited by JOEs_SG Wednesday, February 11, 2015 6:27 PM added powershell version
    Wednesday, February 11, 2015 6:24 PM

Answers

  • This option: ((Get-WmiObject Win32_ComputerSystem).UserName).split('\')[-1] will split the results at the back slash and return the last element.
    • Marked as answer by JOEs_SG Wednesday, February 11, 2015 6:31 PM
    Wednesday, February 11, 2015 6:27 PM

All replies

  • This option: ((Get-WmiObject Win32_ComputerSystem).UserName).split('\')[-1] will split the results at the back slash and return the last element.
    • Marked as answer by JOEs_SG Wednesday, February 11, 2015 6:31 PM
    Wednesday, February 11, 2015 6:27 PM
  • Thanks Tommy,

    Just as you sent that I read about this method on another forum. works just as I wanted. although the method I had was 

     ((Get-WmiObject Win32_ComputerSystem).UserName).split('\')[1]

    Does [-1] work backwards through the string? and 1 work forward? this info would come in handy if there was ever more than one '\'

    Wednesday, February 11, 2015 6:33 PM
  • Yes, negative indexes will read backwards. As you've seen yourself, the 1 indicates the second index because the first index is zero. Positive indexes (to include zero) will start on the left and negative indexes will start on the right. I would tend to use [-1] if I want the last element even if I'm mostly certain they'll only be a single back slash.

    PS C:\> $String = 'Today\is\Wednesday'.Split('\')
    PS C:\> $String[0],$String[1],$String[2]
    Today
    is
    Wednesday
    PS C:\> $String[-3],$String[-2],$String[-1]
    Today
    is
    Wednesday
    Edit: Added 'to include zero'


    • Edited by tommymaynard Wednesday, February 11, 2015 6:47 PM
    Wednesday, February 11, 2015 6:40 PM