Remove i:0#.w|ad\ Prefix using PowerShell and TrimStart RRS feed

  • Question

  • I am trying to remove 


    From accounts listed in the groups in SharePoint 2013. Some of our accounts start with ad, which I obviously want to preserve. However I cannot work out how to only trim the first 'ad'. I cannot prevent a second 'ad' from being trimmed, so "i:0#.w|ad\adxxxx" always gives "xxxx".

    $testString = "i:0#.w|ad\adxxxx";

    Here is a list of what I have tried (yes, many of these are not even close and just symptomatic of my increasing desperation). The 'i:0#.w|' seems to trim just fine on it's own without any escaping, but of course leaves '\ad' which I want to remove.

    $myTrim = 'i:0#.w\|"ad\"?'
    $myTrim = 'i:0#.w|"ad\"?'
    $myTrim = 'i:0#.w|"ad\"{1}'
    $myTrim = "(i:0#.w\|ad\)"
    $myTrim = "(i:0#.w|ad\)"
    $myTrim = "(i:0#\.w\|ad\)"
    $myTrim = "(i:0#\.w\|ad\\)"
    $myTrim = "[i:0#.w\|ad\\]"
    $myTrim = "[i:0#.w|ad\]"
    $myTrim = "<i:0#\.w\|*?\\ad>"
    $myTrim = "<i:0#\.w\|>?<\\ad>"
    $myTrim = "<i:0#\.w\|>*?<\\ad>"
    $myTrim = "<i:0#\.w\|><\?\ad>"
    $myTrim = "<i:0#\.w\|><\\ad>"
    $myTrim = "<i:0#\.w\|><\\ad>(0)"
    $myTrim = "<i:0#\.w\|><\\ad>(1)"
    $myTrim = "<i:0#\.w\|><\\ad>{1}"
    $myTrim = "<i:0#\.w\|ad\\>"
    $myTrim = "i:0#.w\|","ad\?"
    $myTrim = "i:0#.w\|ad\?"
    $myTrim = "i:0#.w|?ad\"
    $myTrim = "i:0#.w|""ad?\"
    $myTrim = "i:0#.w|(ad\)?"
    $myTrim = "i:0#.w|(ad\){1}"
    $myTrim = "i:0#.w|a?d?\?"
    $myTrim = "i:0#.w|ad?\"
    $myTrim = "i:0#.w|ad"
    $myTrim = "i:0#.w|ad\?"
    $myTrim = "i:0#.w|ad\"
    $myTrim = "i:0#\.w\|*?\\ad"
    $myTrim = "i:0#\.w\|ad\?"
    $myTrim = "i\:\0\#\.w\|ad\?"
    $myTrim = "i\:0\#\.w\|*?\\ad"
    $myTrim = "i\:0\#\.w\|ad\?"
    Friday, November 15, 2019 2:04 PM

All replies

  • Don't escape the ? as it tells the pattern to stop matching after the first find.


    Friday, November 15, 2019 3:01 PM
  • Hi,

    Was your issue resolved?

    If you resolved it using our solution, please "mark it as answer" to help other community members find the helpful reply quickly.

    If you resolve it using your own solution, please share your experience and solution here. It will be very beneficial for other community members who have similar questions.

    If no, please reply and tell us the current situation in order to provide further help.

    Best Regards,


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact

    Monday, November 18, 2019 6:44 AM
  • The escape character, '\', is one of the characters I want to match. I'm not trying to escape the ?, I am trying to escape the last '\' in the string after the ad.

    Monday, November 18, 2019 9:27 AM
  • Coming in on Monday morning I found it was a lot easier to simply use split on the '\' to solve the problem.
    Monday, November 18, 2019 1:14 PM
  • To escape the "\" just use two "\\"


    Monday, November 18, 2019 5:44 PM
  • The reason this doesn't work for you is that you're misusing the "TrimStart". The "trim" methods only remove characters. They don't remove matching strings or use regular expressions.

    $testString = "i:0#.w|ad\adxxxx";
    $mytrim = 'i:0#\.w\|ad\\'
    # try using TrimStart
    # does NOT use a regex or match a string -- it only removes CHARACTERS
    Write-Host ("TrimStart: {0}" -f $testString.TrimStart($myTrim))
    # try using regex
    Write-Host ("-Replace:  {0}" -f $teststring -replace $mytrim, '')
    The results of running the example code:
    TrimStart: xxxx
    -Replace:  adxxx
    If you use this example in your script, you should add the "^" at the beginning of the regex string to ensure that it only matches the pattern at the beginning of you data and not somewhere else!


    --- Rich Matheisen MCSE&I, Exchange Ex-MVP (16 years)

    Monday, November 18, 2019 8:14 PM