Author Posts

November 15, 2017 at 5:21 pm

So I know I can do this and get the expanded properties of a value

get-msoluser -userprincipalname  | select -expandproperty licenses

However I want to run through all of my users and see what they are licensed for. I need to use -expand property because some of the users are licensed for multiple items and it doesn't show them all if I don't expand the property. But I can't expand the property and get say UPN so I can link up the username to the licenses.

What's the best way to get both the expanded property and the single valued attribute?

November 15, 2017 at 5:26 pm

You can't really do both at the same time. You'll want to get your users and then enumerate them in a ForEach. That'll get you your user information; within the ForEach, you can expand out licenses on a per-user basis.

It'd be helpful to know what kind of output you're thinking of – I'd be able to offer something more concrete.

November 15, 2017 at 5:57 pm

I figured that was the case.

I just want an output of userprincipalname and licenses, here is an example of what one user looks like if I run this:

get-msoluser -All | select licenses, userprincipalname

Licenses UserPrincipalName
———- ———————–

November 15, 2017 at 5:58 pm

So, I'm guessing what you don't like about the current output is the {} syntax used to show a collection of licenses.

I would go through each user, and expand their licenses. For each license, I'd output a new object having the UPN as one property, and a single license as another property. That will get you something a bit more like what you want.

November 15, 2017 at 6:19 pm

The {} is fine, it's that if there are more than 3 licenses it cuts off the rest of them. I'd actually prefer the whole collection expanded to each individual UPN but I am fine with doing something like this:

UPN License

I think I have some code I hijacked from the internet to give me AD group memberships report that does something similar to this, I will look into it. I'm still very new to custom objects, at least ones that require foreach statements 😉

November 15, 2017 at 6:24 pm

Yeah, so keep in mind that PowerShell wants to deal with objects. It's not a report generator. You could use Group-Object or something to re-group things by user. But the output above is what you're going to need to be happy with ;).

# Assuming $users has your users
# Assuming $users.upn is the UPN, $users.licenses is licenses
ForEach ($user in $users) {
 ForEach ($license in $user.licenses) {
  $props = @{'UPN'=$user.upn
             'License' = $license}
  New-Object -Type PSObject -Prop $props

Something vaguely like that.

November 15, 2017 at 7:51 pm

Thanks Don, that helped a lot. For clarification is the $license variable taking whatever value is in $user.licenses.accountskuid?

For anyone else who might run into this in the future here is what I did to get everything working:

$Users = Get-Msoluser -all 

ForEach ($user in $users) {
 ForEach ($license in $user.licenses.accountskuid) {
  $props = @{'UPN'=$user.userprincipalname
             'License' = $license}
  New-Object -Type PSObject -Prop $props

November 15, 2017 at 7:52 pm

No, it's taking whatever is in $licenses, which would be $user.licenses. You'd need to further enumerate accountskuid or any sub-properties.

November 15, 2017 at 8:00 pm

Ok, that's what I thought. In my code I enumerated further with the accountskuid and got exactly what I needed.

Thanks again for your help.