Search code examples
stringpowershellsplitpowershell-4.0

Splitting strings in PowerShell


I got a variable that contains different strings like domainname/users/username and domainname/accounts/serviceUsers/serviceusername.

Now I want to split these strings on the last "/" so I get only the last bit (username, serviceusername).

I know how to use split when specifiying exactly on which occurance of the "/" the string should be splitted, but not generally at the last "/".

$x = 'domainname/Accounts/ServiceUsers/serviceusername' 
$x.split('/')[3]

How can I achieve that?


Solution

  • Hope this helps

    $x.split('/')[-1]