2

In PowerShell, I'm looking to convert a SharePoint Location string (john_smith_domain_com) to the proper addressing of john.Smith@domain.com. I'm not sure which is the best way to go about doing this. I know $var.Replace("_",".") would work to replace all the "_"s with "."s, but that doesn't help the "@" going between the name and the domain.

Unfortunately, the domain isn't always the same, or I'd be able to just .replace it easily.

Josh B
  • 39
  • 6
  • if the domain is always composed of the last 2 tokens you could deal with it with splitting – Santiago Squarzon Feb 06 '23 at 19:10
  • 2
    can the domains have more than 2 tokens? i.e.: `[mailaddress] 'john.smith@domain.xyz.foo'` would be a valid email address, if so, there is no programmatical way of telling which part is the host and which part is the user of your email address unless you have a list of all valid domains. – Santiago Squarzon Feb 06 '23 at 19:18
  • So, they're valid domains, we just have a bunch of them. My script will not know what domain will come through until it hits this section. So I could have john.smith@domain1.com, john.smith@domain2.com, etc. but never subdomains. My thought was take the second to last _ and make it an @ sign, and replace the rest. – Josh B Feb 06 '23 at 19:26

1 Answers1

3

You can combine two -replace operations (this assumes a two-component domain name, such as domain.com):

# -> 'john.smith@domain.com'
'john_smith_domain_com' -replace '_(?![^_]+_[^_]+$)', '.' -replace '_', '@'
  • Regex _(?![^_]+_[^_]+$) matches all _ chars. except the second-to-last one.

  • After all these have been replaced with ., only the second-to-last one is left, which can then be replaced with @


As JohnLBevan notes, if there are domain names with a varying number of components, you have two options:

  • If you can assume that all users names are in the form <firstName>.<lastName> (as governed by a policy), you can replace the second _ with @:
# -> 'john.smith@domain.co.uk'
'john_smith_domain_co_uk' -replace '^([^_]+)_([^_]+)_', '$1.$2@' -replace '_', '.'
  • Otherwise, as John notes:

You may want something that's aware of the more common TLDs / caters for more common domain formats. Some options are in this post.

mklement0
  • 382,024
  • 64
  • 607
  • 775
  • 1
    Thank you! That did the trick at least for my test user, so I expect it to work for the rest. I knew it was possible with regex, I just haven't been able to grasp the concept of it yet... – Josh B Feb 06 '23 at 19:30
  • NB: It may make sense to assume that the format is always `firstname_lastname` (if that's governed somewhere; e.g. by company policy) rather than assuming `domain_tld` as the the latter may have issues with some tlds (top level domains), like `.co.uk`. Here's one possible alternative for that approach: `'john_smith_domain_co_uk' -replace '_', '.' -replace '^([^\.]+\.[^\.]+)\.', '$1@'`. If you can't rely on `first.last` either, you may want something that's aware of the more common tlds / caters for more common domain formats. Some optoins here: https://stackoverflow.com/q/10306690/361842 – JohnLBevan Feb 06 '23 at 19:43
  • 1
    Thanks, @JohnLBevan - good info; I've added it to the answer (I've tweaked the regex to be more similar the other solution in the answer; as an inconsequential aside: no need to escape a literal `.` as ``\.`` inside `[…]`). – mklement0 Feb 06 '23 at 20:07
  • 1
    Nice one; thanks @mklement0 ; good tip; I always forget where escapes aren't needed / have a bad habit of overescaping. – JohnLBevan Feb 06 '23 at 20:22