powershellexchange-serverpublic-folders

Use modified property of object as parameter in Powershell for Set-MailPublicFolder


When we create a public folder and mail enable in Exchange Online, the default email address is @domain.onmicrosoft.com

Our folder names are "NNNNN_Folder name" where NNNNN is a 5 digit number.

I would like to set the primary email address of the public folder to NNNNN@domain.com

I have tried many variations of this:

Get-PublicFolder -Recurse -Identity "\X\Y\Z"|
    Sort-Object Identity –Descending| 
    Select-Object -first 4|
    Set-MailPublicFolder  -PrimarySmtpAddress {$_.name.substring(0,5)+"@domain.com"}

and receive errors about interpreting the resulting email address:

Cannot process argument transformation on parameter 'PrimarySmtpAddress'. Cannot convert value
"$_.name.substring(0,5)+"@domain.com"" to type "Microsoft.Exchange.Data.SmtpAddress". Error: "The email
address "$_.name.substring(0,5)+"@domain.com"" isn't correct. Please use this format: user name, the @ sign,
followed by the domain name. For example, tonysmith@contoso.com or tony.smith@contoso.com."
    + CategoryInfo          : InvalidData: (:) [Set-MailPublicFolder], ParameterBindin...mationException
    + FullyQualifiedErrorId : ParameterArgumentTransformationError,Set-MailPublicFolder
    + PSComputerName        : outlook.office365.com

I also tried setting the EmailAddress of the PublicFolder to NNNNN@domain.com in the same operation.

-EmailAddresses @{$_.name.substring(0,5)+"@domain.com"}

It doesn't seem to be evaluating the argument or I'm missing something else?

If I change Set-MailPublicFolder ... with % {$_.name.substring(0,5) + "@domain.com"}

I do see the email addresses I am expecting. Thanks,

Craig.


Solution

  • See this version.

    From Microsoft command documentation, the identity parameter is required (see this)

    I am also not sure it can take the array and process each individual without specifying a foreach.

    See this modified versions.

    $PublicFolders = Get-PublicFolder -Recurse -Identity "\X\Y\Z"| Sort-Object Identity –Descending | Select-Object -first 4
    
    $PublicFolders | foreach {
                         $NewEmail = "$($_.name.substring(0,5))@domain.com"
                         Write-Host "Settings MailPublicFolder with name $($_.Identity) to $NewEmail" -ForegroundColor Cyan
                         Set-MailPublicFolder -Identity $_.Identity -PrimarySmtpAddress $NewEmail
                      }