If you want your function to work in all cases, you need to ask for a string array parameter, and enclose your “process” statements in a “foreach ($CurrentUser in $UserAccount) {…}”
For the same reason, your “connect-Msolservice” must be in the “begin” bloc.
And for your question, if you don’t want a PSCustomObject, you could use “select-object” on your $UserADInfo object, with a ‘@{name=“”;expression={}}’ for adding MSOLicense property.
The code is not cleaner with this option, but you still have an ADUser object returned by your function, it may be usefull.
And since you use “ValueFromPipelineByPropertyName”, it’s better to use a real Object property name for your parameter. In your case, a real ADUser property like “userprincipalname” probably.
But, for this to work, the parameter of your function must have the same name of a property of the object return by - for this exemple - get-aduser. For what you want to do, UserPrincipalName seems to be a good choice.
Since you use “ValueFromPipeline”, you could call your function with an array of string before a “|”, like :
@("upn1", "upn2", "upn3") | Get-UserADInfo
In both case, the “begin” code bloc will be execute only one time (with no access to any parameter pass to the function), the process code bloc will be execute for each object/value preceding the “|”, store in the parameter variable, and the “end” code block, only once, at the end.
So, if you don’t want to connect to Msol for each object/value preceding the “|”, you have to put the connect command in the begin bloc.
But, if you call it this way, the “process” code block will be execute only one time, and the value of $UserAccount will be the full array. So, if you want to do the job for each value of the array, the only way is to have :
process {
foreach ($CurrentUser in $UserAccount) {
Do what you want using $CurrentUser
}
}
This will also work when you call your function with the “|”, each execution of “process” bloc will be done with an array containing only one value.