AB_MS3
AB_MS3

Reputation: 77

Troubles with substring, trim, trimend

I'm trying to take an array of email addresses (in the form of [email protected]) 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 [email protected] 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?

Upvotes: 1

Views: 982

Answers (2)

Martin Brandl
Martin Brandl

Reputation: 58931

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 '@.*' }

Upvotes: 3

4c74356b41
4c74356b41

Reputation: 72171

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.

Upvotes: 1

Related Questions