Microsoft Exchange Server 2016 PowerShell Cookbook (2024)

Windows Management Framework (WMF) version 5.1 includes a package manager called PowerShellGet, which enabled functionality such as find, get, install, and uninstall packages from internal and public sources. However, this recipe is not specific to Exchange. See this recipe as a tips and tricks recipe, since it's more PowerShell general than Exchange specific.

PowerShellGet is a package manager for Windows PowerShell. Basically, it is a wrapper around the Package Manager component which simplifies the package management for PowerShell modules. PowerShellGet is built on top of the well-known package management solution, NuGet.

Package Manager is a unified package management component which allows you to search for software install, uninstall, and inventory of any type of software that it supports through the provider interface.

Package Manager works with the community based software repository called PowerShell Gallery. Currently PowerShell Gallery has more than 1,500 unique packages to download and use for free.

Chocolatey is also still available; it's also a community based software repository. Some examples of how to use these can be found in the There's more section.

There are a bunch of galleries (also referred to as providers) to use and select between, such as Chocolatey, PowerShell Resource Gallery (Microsoft supported), MyGet, Inedo ProGet, JFrog Artifactory, and many more.

For a better understanding, let's take a look at the first example.

In this example, we will use PowerShellGet to install two example modules from PowerShell Gallery:

 Import-Module -Name PackageManagement Get-Command -Module PackageManagement Find-Package | Out-GridView Find-Package -Name "AzureAD" Find-Package -Name "GetUptime" Install-Package -Name "AzureAD" Install-Package -Name "GetUptime" Get-Package -ProviderName PowerShellGet 

For illustrating how the Package Manager works, see the preceding example.

First we imported the module of Package Management, for using the cmdlets for the Package Manager. We then used the Get-Command cmdlet to see what commands are available with this module.

With the Find-Package cmdlet, we searched for the available packages. First we piped the results to a GridView, since this can be user friendly to watch instead of text. Once we found the packages we were looking for (in this example Notepad++ and 7zip), we were using the Install-Package cmdlet to install these packages. The following screenshot shows when the installation had taken place, the packages were now available for use and could be found from the start button.

Microsoft Exchange Server 2016 PowerShell Cookbook (1)

Once the packages/modules have been successfully installed they can be imported and utilized by using the following cmdlets:

 Import-Module GetUptime Get-Command -Module GetUptime Get-Uptime 

The following screenshot shows an example of the GetUptime module, where the module shows the server uptime:

Microsoft Exchange Server 2016 PowerShell Cookbook (2)

Once the packages are in place and it has been verified that everything is working as expected, let's finalize this by uninstalling them. Some examples of cmdlets for uninstalling packages are shown here:

 Uninstall-Package -Name "AzureAD" Uninstall-Package -Name "GetUptime" 

We have now been using the built-in package manager based on PowerShellGet. In the previous book called "Microsoft Exchange Server PowerShell Cookbook - Third Edition", we also used Chocolatey, where we can install third-party rich applications.

We'll take a look at how we can utilize chocolatey with Windows 2016 Server and Exchange 2016:

 iwr https://chocolatey.org/install.ps1 -UseBasicParsing | iex choco upgrade chocolatey choco install notepadplusplus.install choco upgrade notepadplusplus.install choco uninstall notepadplusplus.install choco install 7zip.install choco upgrade 7zip.install choco uninstall 7zip.install 

In the preceding examples, we start by installing chocolatey. The second cmdlet is used for upgrading the existing installation of chocolatey. This is followed by two examples of how to install, upgrade, and uninstall two third-party application packages (Notepad++ and 7zip).

Chocolatey is great in many ways, but probably most companies, or at least enterprise companies, want to have their own "internal", more trusted and reliable repository, but still hosted on the internet.

Let's take a look at how this can be established. First let's sign up for an account at an optional provider.

In my case, I've used http://www.myget.org as the provider and created a feed when the account was created.

Now, let's see how the feed can be used as a repository. The feed that was created had an URL of: https://www.myget.org/F/tlpowershell/. Once it's created, we have to register it as a repository in PowerShell using the Register-PSRepository cmdlet:

 Register-PSRepository -Name MyGet -SourceLocation ` https://www.myget.org/F/tlpowershell/api/v1 ` -PublishLocation https://www.myget.org/F/tlpowershell/ ` -InstallationPolicy Trusted Find-Package -Source MyGet 

Since the MyGet repository is brand new, there are currently no packages. So the next action is to upload a package to MyGet. For being able to upload a module, the module itself should have a file extension of .psm1 together with the module manifest using an extension of .psd1. In the manifest, it's necessary to include the values of Author and Description, but I want to recommend that the value of RootModule, ModuleVersion, and CompanyName, are also included. The following examples show how the manifest was created and also how the modules were published to MyGet.

 New-ModuleManifest -Path ` C:\Windows\System32\WindowsPowerShell\v1.0\Modules\mailboxes.psd1` -Author "Jonas Andersson" -CompanyName "Testlabs, Inc." `
-RootModule "mailboxes" -Description `
"Module that lists mailboxes" -ModuleVersion "1.0"
Import-Module PowerShellGet

$PSGalleryPublishUri = ` 'https://www.myget.org/F/tlpowershell/api/v2/package'


$PSGallerySourceUri = ` 'https://www.myget.org/F/tlpowershell/api/v2'
Publish-Module -Name mailboxes -NuGetApiKey `
a2d5b281-c862-4125-9523-be42ef21f55a -Repository MyGet
Find-Package -Source MyGet Install-Package -Name "mailboxes" -Source MyGet

Before ending this recipe, we might want to remove the repository for some reason. This is done simply by running the following cmdlet:

 Unregister-PSRepository -Name MyGet 
  • The Understanding the new execution policy recipe in this chapter
  • The Creating custom objects recipe in this chapter
Microsoft Exchange Server 2016 PowerShell Cookbook (2024)

FAQs

How do I connect PowerShell to Exchange 2016? ›

To use PowerShell to connect to Exchange 2019 or 2016:
  1. Start the PowerShell on the workstation.
  2. In the console, type the following command: $Credentials = Get-Credential. In the resulting window, provide administrative credentials.
  3. Then, enter this command: Import-PSSession $Session.
Nov 20, 2014

What can be used to manage Exchange servers using PowerShell? ›

The Exchange Management Shell is built on Windows PowerShell technology and provides a powerful command-line interface that enables the automation of Exchange administration tasks. You can use the Exchange Management Shell to manage every aspect of Exchange.

How do I get PowerShell on ExchangeServer? ›

How to Install Exchange Online PowerShell Module
  1. Step 1: Set Windows PowerShell Execution Policy. ...
  2. Step 2: Set Up PowerShellGet Module. ...
  3. Step 3: Install the Exchange Online Management module. ...
  4. Step 4: Connect to Exchange Online PowerShell V3. ...
  5. Step 5: Use Commands Available in the Exchange Online Environment.
Dec 6, 2023

How to resolve HTTP 500 error in ExchangeServer 2016? ›

If your server has pending updates that have not been installed, your system can face multiple issues, including the Exchange 2016 EAC not working error 500. To resolve the error, you can update your Windows servers and get rid of any pending updates available.

How do I start Exchange services in PowerShell? ›

Start the services after the computer restarts. To do this, run the following PowerShell command: Get-Service -DisplayName "Microsoft Exchange*" | Where-Object {$_. Starttype -eq "Automatic" -and $_.

How do I log into Exchange using PowerShell? ›

Connecting to Office 365(Exchange Online) via Powershell
  1. Open Powershell as administrator.
  2. Run command – Set-ExecutionPolicy Remotesigned.
  3. Type Y for yes.
  4. Run command – $UserCredential = Get-Credential.
  5. Sign in with O365 administrator account (user@company.com)
  6. Run command – Import-PSSession $Session.

What is the use of PowerShell in Exchange 2016? ›

What is it? EMS (Exchange Management Shell) is based on Microsoft Windows PowerShell, which provides a powerful command-line interface for executing and automating administrative tasks. With the Exchange Management Shell, you can manage every aspect of Exchange Server 2016.

How do I run Exchange PowerShell as administrator? ›

Make sure that the “Administrator: Windows PowerShell” appears in the title bar. If “Windows PowerShell” appears in the title bar instead, right click on the PowerShell icon in the bottom bar and click on “Run as Administrator.”

Which tools can you use to manage Exchange Server 2016? ›

The management tools in Exchange Server 2016 and Exchange Server 2019 include the Exchange Management Shell and the Exchange Toolbox. You can install the management tools on other client computers or servers in the Active Directory domain to help you manage your Exchange organization.

What are the requirements for Exchange Online PowerShell? ›

Prerequisites for connecting to Exchange Online PowerShell

You must have an Exchange Administrator role and credentials for the Office 365 tenant. A computer running Windows 7 or later, Windows Server 2008 R2 or later with PowerShell 5.1 or later. Microsoft . NET Framework 4.7.

Where is the Exchange PowerShell module located? ›

Update the Exchange Online PowerShell module

If the module is installed in C:\Program Files\WindowsPowerShell\Modules, it's installed for all users. If the module is installed in your Documents folder, it's installed only for the current user account.

How to call exchange in PowerShell? ›

Connect to a remote Exchange server
  1. On your local computer, open Windows PowerShell, and run the following command: PowerShell Copy. $UserCredential = Get-Credential. ...
  2. Run the following command: PowerShell Copy. Import-PSSession $Session -DisableNameChecking.
Sep 7, 2023

How do I bypass 500 server error? ›

You can do that by selecting the refresh/reload button, pressing F5 or Ctrl+R, or trying the URL again from the address bar. Even if the 500 Internal Server Error is a problem on the web server, the issue might be temporary. Trying the page again will often be successful.

How do I overcome 500 internal server error? ›

How to Fix 500 Internal Server Error – Website Owner
  1. Reset Permissions. Users might get error 500 on your website if file and folder permissions are incorrect, blocking access to content. ...
  2. Increase PHP Memory Limit. ...
  3. Deactivate WordPress Plugins/Themes. ...
  4. Check Server logs. ...
  5. Check your . ...
  6. Contact Web Hosting Provider.
May 13, 2024

Why do I keep getting server error 500? ›

When you encounter an error 500, it means that the server has encountered an unexpected condition or configuration problem that prevents it from fulfilling the request made by the browser or client. The HTTP status code “500 – Internal Server Error” is one of the many 5.

How to connect to exchange management shell using PowerShell? ›

Connect to a remote Exchange server
  1. On your local computer, open Windows PowerShell, and run the following command: PowerShell Copy. $UserCredential = Get-Credential. ...
  2. Run the following command: PowerShell Copy. Import-PSSession $Session -DisableNameChecking.
Sep 7, 2023

How do I add Exchange to PowerShell? ›

To install the Exchange PowerShell module, you can follow these steps: Open PowerShell as an administrator >> Install the Exchange Online Management module by running the following command: Install-Module ExchangeOnlineManagement >> To verify that the module is installed, run the following command: Get-Module ...

How to connect O365 through PowerShell? ›

How to Connect Powershell to Microsoft 365
  1. Create a PSCredential Object. A PSCredential object stores a username and password that you can use to authenticate to different services. ...
  2. Connect to MSOnline Service. ...
  3. Connect to AzureAD Service. ...
  4. Verify Your Connection.

How do I open Exchange Management Console in PowerShell? ›

  1. Open PowerShell and enter the following command: $LiveCred = Get-Credential.
  2. Enter the login credentials for Exchange when the window appears, and then click "OK."
  3. Enter the following command once the previous command has processed: Remove-PSSession $Session.

Top Articles
Latest Posts
Article information

Author: Duane Harber

Last Updated:

Views: 5456

Rating: 4 / 5 (51 voted)

Reviews: 90% of readers found this page helpful

Author information

Name: Duane Harber

Birthday: 1999-10-17

Address: Apt. 404 9899 Magnolia Roads, Port Royceville, ID 78186

Phone: +186911129794335

Job: Human Hospitality Planner

Hobby: Listening to music, Orienteering, Knapping, Dance, Mountain biking, Fishing, Pottery

Introduction: My name is Duane Harber, I am a modern, clever, handsome, fair, agreeable, inexpensive, beautiful person who loves writing and wants to share my knowledge and understanding with you.