Function within a pipline - Returns wrong value

Occasional Contributor

Hi Everyone ,


I'm trying to create a list of user, and witch Office 365 license they have based on membership in an AD group. I have created a simple function for that myself, and it appears to be working when I test in on a specific user.

I have put the function within a hash table, and the function appears to be running, but the result is wrong (as it is "No License" for all users).


I'm not even sure it's possible to do this this way? But I feel that I'm quite close to something.

Any tips to how I can solve this?

function Get-LicenseType {


$E3Members = Get-ADGroupMember -Identity License-Microsoft-Office365-E3 -Recursive | Select -ExpandProperty Name
$F1Members = Get-ADGroupMember -Identity License-Microsoft-Office365-F1 -Recursive | Select -ExpandProperty Name

if ($E3Members -contains $name) {
Write-Output "E3"
} #End foreach E3

elseif ($F1Members -contains $name) {
Write-Output "F1"
} #End foreach F1

else {
Write-Output "No licence"
} #End function Get-LicenceType

$users = Get-ADUser -Filter {SamAccountName -like "*user*"} -Properties "*"
$users | Select-Object name, mail, SamAccountName, @{n="License";e={Get-LicenseType name}} | Export-Csv C:\PS-Output\$((Get-Date).ToString('yyyyddMM'))-Users.csv -Append -Encoding UTF8 –NoTypeInformation




// Julian

2 Replies
Best Response confirmed by Julian Revheim Askeland (Occasional Contributor)

Well for starters, your list of users has a strict filter that will only return the ones with "user" in their samaccountname, are you sure you have enough matches and are those users actually members of the two groups? Next, your function doesn't return any value, instead it writes output. Change it to use the return statement. Well technically both should work, but I prefer "return" :)


Lastly, when using calculated properties, you need to use the $_.Name format instead of just "name". So this:


$users | Select-Object name, mail, SamAccountName, @{n="License";e={Get-LicenseType $}} | Export-Csv C:\PS-Output\$((Get-Date).ToString('yyyyddMM'))-Users.csv -Append -Encoding UTF8 –NoTypeInformation



Thank you Vasil!


It actually worked when I changed to $ :)
But I like to make my small function better, so I will add "return" to my script  aswell.


Sorry to put you of by my filter, I just added "user" as an example.


I now have the list I need! Thanks again! :)


// Julian