SOLVED

Change email domain in bulk via powershell

Copper Contributor

Previously I'm able to run this command to mass change email domain via powershell:

------------------------------------------------------------------------------------------------------

Import-CSV C:\temp\MigrateOld2NewDomain.csv | %  {

Set-Mailbox $_.ExistDomain -WindowsEmailAddress $_.NewDomain -MicrosoftOnlineServicesID $_.NewDomain

}

------------------------------------------------------------------------------------------------------

But today when i run it, encounter error:

------------------------------------------------------------------------------------------------------

Set-Mailbox : The term 'Set-Mailbox' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or 

if a path was included, verify that the path is correct and try again.

-------------------------------------------------------------------------------------------------------

I connected to Connect-ExchangeOnline, and seems to have issue connect to remote server (not sure whether this is the cause on why the script fail)

-------------------------------------------------------------------------------------------------------

PS C:\WINDOWS\system32> Connect-ExchangeOnline

----------------------------------------------------------------------------

The module allows access to all existing remote PowerShell (V1) cmdlets in addition to the 9 new, faster, and more reliable cmdlets.

|--------------------------------------------------------------------------|

|    Old Cmdlets                    |    New/Reliable/Faster Cmdlets       |

|--------------------------------------------------------------------------|

|    Get-CASMailbox                 |    Get-EXOCASMailbox                 |

|    Get-Mailbox                    |    Get-EXOMailbox                    |

|    Get-MailboxFolderPermission    |    Get-EXOMailboxFolderPermission    |

|    Get-MailboxFolderStatistics    |    Get-EXOMailboxFolderStatistics    |

|    Get-MailboxPermission          |    Get-EXOMailboxPermission          |

|    Get-MailboxStatistics          |    Get-EXOMailboxStatistics          |

|    Get-MobileDeviceStatistics     |    Get-EXOMobileDeviceStatistics     |

|    Get-Recipient                  |    Get-EXORecipient                  |

|    Get-RecipientPermission        |    Get-EXORecipientPermission        |

|--------------------------------------------------------------------------|

To get additional information, run: Get-Help Connect-ExchangeOnline or check https://aka.ms/exops-docs

Send your product improvement suggestions and feedback to email address removed for privacy reasons. For issues related to the module, contact Microsoft support. Don't use the feedback alias for problems or support issues.

----------------------------------------------------------------------------

New-ExoPSSession : Connecting to remote server outlook.office365.com failed with the following error message :  For more information, see the 

about_Remote_Troubleshooting Help topic.

-------------------------------------------------------------------------------------------

12 Replies
I think you have to update your module. The information you get after connecting should show v3. Update by update-module ExchangeOnlineManagement , it should be version 3.2.0 afterward (Check your current version with get-module ExchangeOnlineManagement
get-module ExchangeOnlineManagement

ModuleType Version PreRelease Name ExportedCommands
---------- ------- ---------- ---- ----------------
Script 3.2.0 ExchangeOnlineManagement {Add-VivaModuleFeaturePolicy, Get-ConnectionInfor…
)

@AnthonyChan_5ming 

 

As @Harm_Veenstra  mentions, first ensure that your Exchange Online PowerShell module is upto date:

Get-InstalledModule ExchangeOnlineManagement | Format-List Name,Version,InstalledLocation
Update-Module -Name ExchangeOnlineManagement

https://learn.microsoft.com/en-us/powershell/exchange/exchange-online-powershell-v2?view=exchange-ps...

 

Next would be to check/confirm that the account you're using has the relevant Exchange RBAC permissions:

Get-ManagementRole -Cmdlet Set-Mailbox
Get-ManagementRoleAssignment -RoleAssignee <userIdentity>

@Harm_Veenstra 

I've run the update as follow, but still encounter same error:

-----------------------------------------------------------------------------------------------------

PS C:\WINDOWS\system32> update-module ExchangeOnlineManagement

PS C:\WINDOWS\system32> Get-InstalledModule ExchangeOnlineManagement | Format-List Name,Version,InstalledLocation


Name : ExchangeOnlineManagement
Version : 3.2.0
InstalledLocation : C:\Program Files\WindowsPowerShell\Modules\ExchangeOnlineManagement\3.2.0


PS C:\WINDOWS\system32> C:\Users\admin\OneDrive - XYZ\O365 - Migrate from old to new domain.ps1
Set-Mailbox : The term 'Set-Mailbox' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or
if a path was included, verify that the path is correct and try again.

-----------------------------------------------------------------------------------------------------

@kevkelly
Which module should I connect to run Get-ManagementRole?
----------------------------------------------------------------------------------------------
Get-ManagementRole : The term 'Get-ManagementRole' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of
the name, or if a path was included, verify that the path is correct and try again.
-------------------------------------------------------------------------------------------------
Did you start a new session and rerun the Connect-ExchangeOnline cmdlet? Afterward, the Set-Mailbox cmdlet should be available
@Harm_Veenstra
Yes, I've close and open new Window Powershell ISE as admin. This error regarding 'New-ExoPSSession' seems to be the culprit when i connect to ExchangeOnline, but i not sure what to do

---------------------------------------------------------------------------------------------------
PS C:\WINDOWS\system32> Connect-ExchangeOnline

----------------------------------------------------------------------------
The module allows access to all existing remote PowerShell (V1) cmdlets in addition to the 9 new, faster, and more reliable cmdlets.

|--------------------------------------------------------------------------|
| Old Cmdlets | New/Reliable/Faster Cmdlets |
|--------------------------------------------------------------------------|
| Get-CASMailbox | Get-EXOCASMailbox |
| Get-Mailbox | Get-EXOMailbox |
| Get-MailboxFolderPermission | Get-EXOMailboxFolderPermission |
| Get-MailboxFolderStatistics | Get-EXOMailboxFolderStatistics |
| Get-MailboxPermission | Get-EXOMailboxPermission |
| Get-MailboxStatistics | Get-EXOMailboxStatistics |
| Get-MobileDeviceStatistics | Get-EXOMobileDeviceStatistics |
| Get-Recipient | Get-EXORecipient |
| Get-RecipientPermission | Get-EXORecipientPermission |
|--------------------------------------------------------------------------|

To get additional information, run: Get-Help Connect-ExchangeOnline or check https://aka.ms/exops-docs

Send your product improvement suggestions and feedback to email address removed for privacy reasons. For issues related to the module, contact Microsoft support. Don't use
the feedback alias for problems or support issues.
----------------------------------------------------------------------------

New-ExoPSSession : Connecting to remote server outlook.office365.com failed with the following error message : For more information, see the
about_Remote_Troubleshooting Help topic.
At C:\Users\adm_acch006350\Documents\WindowsPowerShell\Modules\ExchangeOnlineManagement\2.0.5\netFramework\ExchangeOnlineManagement.psm1:475 char:30
+ ... PSSession = New-ExoPSSession -ExchangeEnvironmentName $ExchangeEnviro ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ResourceUnavailable: (:) [New-ExoPSSession], PSRemotingTransportException
+ FullyQualifiedErrorId : System.Management.Automation.Remoting.PSRemotingDataStructureException,Microsoft.Exchange.Management.ExoPowershellSnapin.NewExoPSSession
--------------------------------------------------------------------------------------------------------
I still see the older version (It mentions V1 instead of V3 and Modules\ExchangeOnlineManagement\2.0.5\). Are you sure it's updated to the latest version? Perhaps a remove-module ExchangeOnlineManagement followed by uninstall-module ExchangeOnlineManagement would be better to remove the old version(s). Then you can use the install-module ExchangeOnlineManagement and Connect-ExchangeOnline again

@AnthonyChan_5ming 

 

As @Harm_Veenstra mentioned, you may have added 3.2.0 but it's clearly not being used. Version 2.0.5 is.

 

The only time I've seen a lower version of a module (I can't remember which module) take precedence over a later release - without being explicitly directed to do so - is when there was an x86 version installed. Because the x86 path is searched before the x64 path, this led to the older module being loaded instead of the newer module.

 

Remove all versions of the ExchangeOnlineManagement module

 

 

# This needs to be run with administrative privileges.
Uninstall-Module -Name ExchangeOnlineManagement -AllVersions -Force

 

 

Install the ExchangeOnlineManagement module

 

 

# This needs to be run with administrative privileges.
Install-Module -Name ExchangeOnlineManagement -Scope AllUsers -Force

 

 

Check that only the x64 version is installed

 

 

Get-Module -ListAvailable -Name ExchangeOnlineManagement

 

 

 

LainRobertson_0-1690197636553.png

 

Separately, something did change on the service side that impacted 2.0.5, as I'd been running that module for around perhaps 18 months due to a bug in the 3.x modules which I had to re-investigate last Thursday, when 2.0.5 broke.

 

My gut feeling is that there's no going back to 2.0.5, so you really will need to get yourself onto the 3.x version of ExchangeOnlineManagement.

 

Cheers,

Lain

I'm running the 2 command:
remove-module ExchangeOnlineManagement
uninstall-module ExchangeOnlineManagement
then it is in this mode 'running script/selection. Press Ctrl+Break to stop. Press Ctrl+B to break into debugger.' for almost an hour now. Is it normal?
best response confirmed by AnthonyChan_5ming (Copper Contributor)
Solution
You can stop and close powershell, remove C:\Users\adm_acch006350\Documents\WindowsPowerShell\Modules\ExchangeOnlineManagement\2.0.5 and try to install it again using nstall-module ExchangeOnlineManagement
@Harm_Veenstra: This does the trick! After remove from the folder manually and install the new module, I'm able to run my script perfectly now. Thank you!

@Lain: Thank you for your advice too. :)
Nice, good to hear :smiling_face_with_smiling_eyes: please mark my answer as solution to mark it as solved
1 best response

Accepted Solutions
best response confirmed by AnthonyChan_5ming (Copper Contributor)
Solution
You can stop and close powershell, remove C:\Users\adm_acch006350\Documents\WindowsPowerShell\Modules\ExchangeOnlineManagement\2.0.5 and try to install it again using nstall-module ExchangeOnlineManagement

View solution in original post