Exchange Online PowerShell V2 use

Microsoft updated Exchange Online Powershell V2 at the beginning of the year. Because of the epidemic, I didn't pay attention to it. After all, the old version of the script can continue to run. Today, I was just free. I tested the new version and found that the stability and speed were greatly improved. Later, I changed the script of the old version.

Let's see how the new version works.


#The purpose of the first line is to change the security protocol to tls1.2. The default tls1.0 of PS5 is no longer supported. If you directly use install module, an error will be reported

[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 

#The following two lines are used to install or update the installation module of PowerShell Gallery
Install-Module -Name PowerShellGet -Force
Update-Module -Name PowerShellGet

#The following two lines are used to install or update the exchange online management module. For the old version, we need to log in to Exchange Admin to download it. The new version can be installed directly through PowerShell Gallery

Install-Module -Name ExchangeOnlineManagement
Update-Module -Name ExchangeOnlineManagement

#After installation, import and view module information
Import-Module ExchangeOnlineManagement; Get-Module ExchangeOnlineManagement

After installation, you can connect and run directly. Compared with the first version, version 2.0 is much better. In the old version, we used new pssession to connect, but when I used MFA, I could only call dll file indirectly to operate, which was very disgusting. Each link needed to input mobile phone verification code, and it was very unstable. Executing script for more than 20 minutes would lead to complete crash of PowerShell ISE.

The new version can pass the credential link directly, which is very convenient and stable.

You can make a comparison. Here is the 1.0 method

#MFA Authentication´╝î It works, but it's unstable#
    Import-Module $((Get-ChildItem -Path $($env:LOCALAPPDATA+"\Apps\2.0\") -Filter Microsoft.Exchange.Management.ExoPowershellModule.dll -Recurse ).FullName|?{$_ -notmatch "_none_"}|select -First 1)
    $EXOSession = New-ExoPSSession
    Import-PSSession $EXOSession

#Normal login

    $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri -Credential $Credential -Authentication Basic -AllowRedirection
    Import-PSSession $Session
    Connect-MsolService -Credential $credential

The following is the way of 2.0, but my test found that my MFA administrator account works even if it is directly connected in the normal way

Connect-ExchangeOnline -Credential $UserCredential -ShowProgress $true

Connect-ExchangeOnline -UserPrincipalName $UPN -ShowProgress $true

The connection effect is neat. Compared with 1.0, it's almost twice as fast. There's not a lot of confusing warning messages

Finally, let's see the relevant command changes


At first glance, it seems that there is an EXO in front of the command. For example, from get mailbox to get exomailbox, its internal parameters have changed. The old version of get mailbox will return all the attributes, including a large number of blank attributes. These attributes may be used very rarely, which wastes a lot of resources and time. In the new version, he has defined a thing called propertyset, that is, to put all kinds of common properties into large categories. When we return, we can specify the corresponding propertyset to get the corresponding information, rather than all of them.

such as

Although it doesn't seem to improve much, the optimization of connection and execution effect really feels refreshing and too much

Tags: Session Mobile

Posted on Fri, 22 May 2020 02:21:25 -0700 by shonuff