Use PowerShell to manage Exchange Online
Its Master's Voice
In a PowerShell session, admins can configure settings for Exchange Online and on-premises Exchange servers in parallel. Therefore, hybrid environments also benefit from the scripting language's possibilities. In this article, I introduce you to managing Exchange Online.
To manage Exchange Online from a computer with PowerShell, the computer must have a cloud connection and the required PowerShell modules, unless you only want to administer Exchange Online without on-premises Exchange servers, in which case you would be working remotely across the board. If you want to work with on-premises Exchange servers in parallel, you need to install the latest version of the Exchange management tools on the appropriate computer.
Configuring Local Settings with PowerShell
To manage Exchange Online, the local execution policy for PowerShell must be set to RemoteSigned
instead of Restricted
. If it is set to Restricted
, PowerShell will not run any scripts. RemoteSigned
trusts the scripts required for a remote connection. The first step in managing Exchange Online with PowerShell is to set the execution policy accordingly. On Windows 10 computers, you do this by starting the PowerShell session with administrative privileges from the context menu.
Depending on the execution policy settings, PowerShell blocks unsigned scripts. You can use the Set-ExecutionPolicy
cmdlet to change the execution policy and Get-ExecutionPolicy
cmdlet to view it. For example, to customize the execution policy on a server or a computer, use the
Set-ExecutionPolicy RemoteSigned
command. The command
Set-ExecutionPolicy Unrestricted
disables the execution policy completely. However, this action is not recommended, because it means any script can be run. However, if certain scripts cause problems, you can test the various execution policy settings:
Restricted
: No scripts are permitted.AllSigned
: Only signed scripts are permitted.RemoteSigned
: Scripts need to be signed by a certificate authority, and it is the default setting for Windows Server 2016 and 2019.Unrestricted
: All scripts can be run.
PowerShell Core versus PowerShell
In parallel with the standard version of PowerShell, Microsoft offers the open source Core variant [1], a newly developed platform-independent version of the scripting language. PowerShell Core is based on .NET Core.
PowerShell Core 6.x can be installed on Windows computers with Windows 7 or higher, but the Core variant generally offers fewer functions than the full version of PowerShell. For example, it lacks extensions and options for accessing devices by Windows Management Instrumentation (WMI). Microsoft is constantly working on the Core version of PowerShell. The plan, in the long run, is that all PowerShell modules will be available in PowerShell Core 6.x. In the meantime, many tasks can be carried out with the Core version without problem. The current PowerShell 5.1 will be the last version to be developed only for Windows. Microsoft's primary goal is to make all new versions platform independent, and PowerShell 6.0 will become the standard version for new Windows systems.
PowerShell Core 6.x can be installed on Windows clients as well as servers running Windows Server 2008 R2 or later. To install PowerShell Core 6.x on Linux machines, you need Ubuntu (14.04 or 16.04), Debian 8, CentOS 7, Red Hat Enterprise Linux 7, openSUSE 42.1, or Arch Linux. For other distributions, Microsoft offers a separate installation package on GitHub. On macOS machines, you need at least macOS 10.12. Additionally, PowerShell Core 6.x can also run in Docker containers and on the Kali Linux security distribution.
Before you can install PowerShell Core 6, you have to install the .NET Core Framework [2] on the corresponding computer. As with the previous PowerShell 5.x version, PowerShell Core 6 supports get-command
to display the full command set, so you can quickly see which commands can be used. The command works on Windows, macOS, and Linux computers. Once PowerShell Core is installed on a Linux machine, for example, type:
sudo apt-get update sudo apt-get install -y powershell
In SSH sessions (e.g., with Putty), you can launch PowerShell with the powershell
command, which extends the capabilities of Linux computers if Windows servers are used on the network. Also, admins who are new to Linux can use familiar tools such as PowerShell to manage Linux servers.
To view the installed version, run the PowerShell $PSVersionTable
command. The Get-Variable ls*
command also displays information about the installed version. For a list of all modules with which the current version of the installed PowerShell is compatible, use:
Get-Module -ListAvailable
In the future, PowerShell will use SSH to establish remote connections. Currently, winrm
is used for this purpose. By way of example, to establish a remote session, type the command:
Enter-PSSession -Hostname <IP or FQDN> -Username <username> -SSHTransport
PowerShell also includes the PowerShell Integrated Scripting Engine (ISE). In the future, however, it is expected that a graphical user interface will be available for PowerShell Core 6, probably based on Visual Basic code. Like PowerShell Core, the development environment is available for free.
PowerShell Core is suitable for managing Exchange Online, as well. The big innovation since PowerShell Core v6.1 is the ability to integrate Windows modules. Because Microsoft is no longer actively pursuing the development of PowerShell (as opposed to PowerShell Core), removing obstacles to the introduction of PowerShell Core has become even more important. If you already have PowerShell Core installed and want to use the latest version, run
Invoke-Expression "& { $(irm https://aka.ms/install-powershell.ps1) } -UseMSI"
to update directly in PowerShell.
Opening a Connection to Exchange Online
Whether you use the traditional PowerShell or PowerShell Core to connect, and whether you use a graphical environment such as PowerShell ISE or Visual Studio Code, the connection process is the same. The first step is to store the credentials of an administrator who has the right to connect to Exchange Online in a variable:
$user = Get-Credential
Storing in a variable has the advantage that you can authenticate quickly, because you only have to insert the variable at the right place and then set up a session for Exchange Online. You will want to save the session in a variable, because it lets you import the session in an active PowerShell window:
$session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $user -Authentication Basic -AllowRedirection
If you don't see an error message, you can import the stored session into PowerShell or PowerShell Core with
Import-PSSession $session
and work with Exchange Online.
This command imports the Exchange Online management cmdlets into the session and makes them available. However, to manage Exchange Online with PowerShell permanently, you might want to use PowerShell ISE or the Visual Studio Code graphical user interface, which will give you more control and provide more tools.
Buy this article as PDF
(incl. VAT)
Buy ADMIN Magazine
Subscribe to our ADMIN Newsletters
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Most Popular
Support Our Work
ADMIN content is made possible with support from readers like you. Please consider contributing when you've found an article to be beneficial.