0

I'm attempting to remove everything after and including the last slash in a CanonicalName. As you might expect that amount of letters, characters, words and the length are variable. Which is why I need to use the fact that everything after the last slash "/" is the only thing each one has in common.

I'm using the following:

(Get-ADUser Identity USER -Properties *).CanonicalName

Normal output would be something like:

domain.tld/dept/section/office/USER

What I would like the result to be is:

domain.tld/dept/section/office

Eventually this will be incorporated into for loop and run through thousands of users which is why I need to find a way to strip off the end.

xocela
  • 3
  • 2
  • https://stackoverflow.com/questions/51382576/how-to-get-the-index-of-the-last-occurence-of-a-char-in-powershell-string – Uuuuuumm Nov 09 '20 at 19:58

1 Answers1

1

You can do the following:

(Get-ADUser Identity USER -Properties CanonicalName).CanonicalName -replace '/[^/]*$'

/ matches literal /. [^] is a negated character class making [^/] match anything but /. * is zero or more matches quantifier (greedily matching). $ is the end of string.


Path conversions do work as well, but if your platform's directory separator char is not /, then the / will be converted to something else. Then you will be left with converting them back. It is just more work than using the simple -replace operator.

AdminOfThings
  • 23,946
  • 4
  • 17
  • 27