1

I'm trying to take an array of email addresses (in the form of username@company.com) which is generated from:

$users = get-MSOLUser -All | where {$_.isLicensed -eq "TRUE" -and $_.Licenses.AccountSKUID -eq "my_license"} | select userprincipalname

And get just the username from each. I start with username@company.com and want to end up with username. I have tried various ways using substring, Trim, TrimEnd, etc and can't get any of them working.

$username = $users | %{$_.substring(0,$users.length - 12)}
$users | %{$_.trimend("@company.com")}
$users | %{$_.trimend(12)}

All of the above give errors including the two below.

Method invocation failed because [Selected.Microsoft.Online.Administration.User] does not contain a method named substring.

Method invocation failed because [Selected.Microsoft.Online.Administration.User] does not contain a method named trimend.

What am I doing wrong with the syntax, or is there something else, like a module I haven't imported, or how my syntax is trying to work with an array?

4c74356b41
  • 69,186
  • 6
  • 100
  • 141
AB_MS3
  • 77
  • 1
  • 3
  • 14

2 Answers2

3

This will return you a list of all usernames (without domain) that fulfills your conditions:

$users = Get-MSOLUser -All | 
    Where-Object {$_.isLicensed -eq "TRUE" -and $_.Licenses.AccountSKUID -eq "my_license"} | 
    ForEach-Object { $_.userprincipalname -replace '@.*' }
Martin Brandl
  • 56,134
  • 13
  • 133
  • 172
1

Well, you need to work with the property, not with the object, so you would probably want to do something like:

select -expandproperty userprincipalname

but that would create an array of userprincipalnames, so no other attributes.

When you run get-MSOLUser you get back an object, with a bunch of properties. When you do select -expandproperty you are getting back only certain property, but not an object itself. You are getting back a system.string object. And that object has all those methods you are trying to invoke.

4c74356b41
  • 69,186
  • 6
  • 100
  • 141
  • adding -expandproperty to my statement totally worked. I guess I should read up on how that works. Thank you very much. – AB_MS3 Nov 30 '16 at 19:24
  • well, let me elaborate in my answer a bit – 4c74356b41 Nov 30 '16 at 19:25
  • Starting off with BSmith@company.com. Any idea why $users | %{$_.trimend("@company.com")} returns (sometimes) BSmit, but $users | %{$_.trimend("ompany.com")} returns BSmith@c? Having @ or not makes a big difference in results. – AB_MS3 Nov 30 '16 at 20:34
  • what do you mean sometimes? O_o anyway, the quirk with `trim` is that it deletes all the characters. Say `'aaaa'.Trimend('a')`, will delete everything, but `'aaaba'.Trimend('ba')` will also delete everything, so it deletes all the occurrences of the characters in the `Trim` method. – 4c74356b41 Nov 30 '16 at 20:41
  • In the array of strings (username@company.com) when I TrimEnd ("@company.com") for some of the strings, it cuts off the last 1 to couple characters (BSmi). If I test that list with TrimEnd("company.com") or less ("ompany.com") the result is BSmith@ or BSmith@c. Does that make sense? – AB_MS3 Nov 30 '16 at 20:46
  • It can do that, it will `trim` all the characters (that are passed to the `trim` method) it can get to. So your company name should have both `t` and `h` in it. to understand that test `trim` with various strings: like `'aa'.Trim('a')`,`'ababa'.Trim('a')`, `'abacacba'.Trim('ab')` and so on – 4c74356b41 Nov 30 '16 at 21:09