Use cmdlet output as variables for next cmdlet

Copper Contributor

I am trying to assign users a customattribute in Exchange for use in a Dynamic Distribution List. We are pure Azure AD (no on-prem or Hybrid); therefore, my DDL options are limited).


I cannot get PowerShell to assign the customattribute based on the output of the get-azureaduser command. Here's what I have, and where it's failing.


This produces a list of user email addresses that I want to use in the next step. This works as expected.



$Var1 = Get-AzureADUser -all $true | Where-Object -property "Foo" -eq "Bar" |  ft -hidetableheaders UserPrincipalName




I'm trying to update each user with the customattribute1. I've tried multiple variations of this procedure, each failing.



$Var1 | foreach-object {
set-mailbox -identity $_ -customattribute1 "Some_Text"



The set-mailbox command works as intended when executed normally (outside of the script), so I'm pretty confident the syntax is correct.


The error I keep getting is:



Cannot process argument transformation on parameter 'Identity'. Cannot convert value "Microsoft.PowerShell.Commands.Internal.Format.FormatEntryData" to type
"Microsoft.Exchange.Configuration.Tasks.MailboxIdParameter". Error: "Cannot convert the "Microsoft.PowerShell.Commands.Internal.Format.FormatEntryData" value of type
"Deserialized.Microsoft.PowerShell.Commands.Internal.Format.FormatEntryData" to type "Microsoft.Exchange.Configuration.Tasks.MailboxIdParameter"."
    + CategoryInfo          : InvalidData: (:) [Set-Mailbox], ParameterBindin...mationException
    + FullyQualifiedErrorId : ParameterArgumentTransformationError,Set-Mailbox
    + PSComputerName        :




I'm lost on the "cannot convert value ... to type ...".  FWIW, my background is Bash. This would be a simple for-each loop in Bash. What am I doing wrong?


Thank you for your time.

3 Replies
best response confirmed by ShawnEG (Copper Contributor)
I think the "| ft -hidetableheaders UserPrincipalName" is something you shouldn't use. Run it without that and try

$Var1 | foreach-object {
set-mailbox -identity $_.UserPrincipalName -customattribute1 "Some_Text"

If that doesn't work, just run $var1 to show us the contents of that

When I run it with "| ft -hidetableheaders UserPrincipalName" it spits out a list of email addresses, e.g,
Email address removed
Email address removed

When I remove the "ft" portion it spits out all the info for each mailbox, e.g.
ObjectId DisplayName UserPrincipalName UserType
-------- ----------- ----------------- --------
abc123def-ab12-12ab-34cd-efghijk Patrick Star Email address removed Member

When I try to add the customattribute I get massive error codes for each line item. The beginning of each error code starts with:

Cannot process argument transformation on parameter 'Identity'. Cannot convert value "class User {


EDIT: NEVERMIND! It worked. I didn't catch the $_.UserPrincipalName at first. After I replied I noticed it and tried again. It worked. Thank you very much!!!

:beaming_face_with_smiling_eyes: No problem, glad to help