I'm running a script in exchange management shell, using variables to output a command for creating user email accounts. Write-Host does a nice job showing the command string in the management shell, but of course does not execute it. I've had success using Invoke-Expression followed by a variable representing a string using commandlets such as New-Addresslists and New-Accepteddomain, but when using Invoke-Expression with a variable containing the New-Mailbox commandlet, I get error messages. How do I properly run a variable string from scripts specifically into the exchange management shell to create user email accounts?
Powershell / Exchange 2019
$COMMAND = "New-Mailbox -Name $CustomerFirstName $CustomerLastName$ -UserPrincipalName $EmailPrefix@$CustomerDomainName -Password (ConvertTo-SecureString -String '$CustomerMailPassword' -AsPlainText -Force) -FirstName $CustomerFirstName -LastName $CustomerLastName"
Invoke-Expression $COMMAND