• Home
  • Topics
    • Office 365
    • Teams
    • SharePoint Online
    • Exchange 2019
    • Exchange 2016
    • Exchange 2013
    • Hybrid
    • Certificates
    • PowerShell
    • Migration
    • Security
    • Azure
  • Blog
  • Podcast
  • Webinars
  • Books
  • About
  • Videos
    • Interview Videos
    • How To Guide Videos
  • Subscribe
    • Facebook
    • Twitter
    • RSS
    • YouTube

Practical 365

You are here: Home / Exchange Server / PowerShell Function to Connect to Exchange On-Premises

PowerShell Function to Connect to Exchange On-Premises

December 1, 2015 by Paul Cunningham 16 Comments

Connecting to Exchange on-premises using PowerShell remoting is a simple task, and means that you do not need to go the trouble of installing the Exchange management tools on your computer just to perform day to day administrative tasks.

There are three steps to establishing a remote PowerShell session to your Exchange server:

  • Capture admin credentials
  • Create a new PSSession
  • Import the PSSession

TechNet has the steps documented, but I prefer to use a PowerShell function in my profile to avoid typing out the steps manually.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
Function Connect-Exchange {
 
    param(
        [Parameter( Mandatory=$false)]
        [string]$URL="ex2016srv1.exchangeserverpro.net"
    )
    
    $Credentials = Get-Credential -Message "Enter your Exchange admin credentials"
 
    $ExOPSession = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://$URL/PowerShell/ -Authentication Kerberos -Credential $Credentials
 
    Import-PSSession $ExOPSession
 
}


In the example above I use a default URL value for quickly connecting to my preferred server, but can override it with the -URL parameter when I run the function.

After adding the function to your PowerShell profile and opening a new PowerShell console you simply run Connect-Exchange to establish a new remote PowerShell session to your Exchange server.

1
PS C:\> Connect-Exchange


As I mentioned earlier you can specify a different server with the -URL parameter.

1
PS C:\> Connect-Exchange -URL ex2016srv2.exchangeserverpro.net


This function is also available on Github.

Exchange Server Exchange 2010, Exchange 2013, Exchange 2016, PowerShell

Comments

  1. Geetha Karuturi says

    August 5, 2021 at 1:33 pm

    Hi Paul,
    Which $Credentials are you using On perm or Exchange ??

    Reply
    • Tony Redmond says

      August 6, 2021 at 9:33 am

      The post is about connecting to Exchange on-premises.

      Reply
  2. Garrett says

    August 24, 2019 at 6:11 am

    Love your site Paul. Can you talk about using Import-PsSession vs. Export-PsSession when it comes to Exchange remoting? Could there be a performance advantage there?

    Reply
  3. Javier says

    September 14, 2017 at 3:27 am

    Hi Paul

    the information help me to a lot but I have a question, It is possible to configure the VIP instead using the FQND of Exchange server?

    when I tried to configure the VIP I got error and error with the WinRM
    “[VIP.domain.com] Connecting to remote server failed with the following error message : WinRM cannot process the request. The following error occured while using Kerberos authentication: The network path was not found.”

    Reply
    • Paul Cunningham says

      September 19, 2017 at 10:07 am

      Have you set up kerberos auth for your Exchange servers?

      Reply
      • Javier says

        October 13, 2017 at 1:59 am

        I read the article from MS but it is possible to do it with Basic authentication? I opened a case with MS they said everything is fine and they asked me to use Basic instead Kerberos as that will be more work, they also suggested round robin DNS however that is not what I am looking for but I keep that as an option, i will give a try and enable the kerberos, thanks for the response,

        Have a great day

        Reply
  4. Shane Wright says

    June 23, 2017 at 6:29 am

    Hey Paul,

    I added some features to your connect-exchange cmdlet including:

    Automatically find the powershell remoting server
    list all powershell remoting servers and choose one to connect
    list all powershell remoting servers by site or version and choose one to connect

    https://gallery.technet.microsoft.com/Connect-Exchange-740ed2b6

    Hope you like it!

    Shane

    Reply
  5. MSMS MSMS says

    June 8, 2017 at 6:38 pm

    How do you handle working with deserialized objects? e.g. Get-ExchangeServer | Get-MessageTrackingLog ?

    Best regards, MSMS MSMS

    Reply
  6. Off2w0rk says

    February 6, 2017 at 5:40 am

    Hi Paul,

    thanks for the tip!
    I’m not a scripter, but can read and modify some. Currently we administer Exchange for several customers in our environment. How can we easily modify your script so it does the following:

    Press 1 to connect to server 1
    Press 2 to connect to server 2
    Press 3 to connect to server 3

    and so on?

    thanks and keep up the good work!

    Reply
    • Lasse Larsen says

      April 10, 2017 at 6:50 pm

      You can use the Switch{} system for that.
      Create a menu with Write-host.

      Then
      $Choice = Read-host Please choose from list
      Switch{
      1{ Connect-exchange -URL xxxxxxxxx}
      2{ Connect-exchange -URL xxxxxxxxx}
      3{ Connect-exchange -URL xxxxxxxxx}
      }

      Reply
      • Lasse Larsen says

        April 10, 2017 at 6:51 pm

        And I forgot to add Switch($Choice){ 🙂

        Reply
  7. Barron says

    October 26, 2016 at 2:03 am

    Hi Paul,

    Thanks for the information, I have one question, how the session will end, according to technet with Remove-PSSession $Session ends the current sesion but that will not work with your script, if I close powershell the session will ends??

    thanks

    Reply
    • Paul Cunningham says

      October 26, 2016 at 12:06 pm

      Get-PSSession will list your current session so you can work out which one to remove. Or just close PowerShell.

      Reply
  8. Jack P says

    December 5, 2015 at 6:41 am

    Good stuff!

    I use to create another version. It is a simple pass through without any prompt for read only stuff…

    Function Connect-Exchange2 {

    param(
    [Parameter( Mandatory=$false)]
    [string]$URL=”ex2016srv1.exchangeserverpro.net”
    )

    # it passes the cred of the current logon user
    $ExOPSession = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://$URL/PowerShell/ -Authentication Kerberos

    Import-PSSession $ExOPSession

    }

    Reply

Leave a Reply Cancel reply

You have to agree to the comment policy.

Recent Articles

  • Changes in Microsoft 365 Apps Channels and Why You Should Care
  • A New Tool to Manage Exchange-related Attributes Without Exchange Server
  • Microsoft Launches Group Ownership Governance Policy
  • Making the Case for Identity Governance in Azure Active Directory
  • Prepare an Office 365 migration plan assessment using PowerShell

Copyright © 2022 Quadrotech Solutions AG · Disclosure · Privacy Policy
Alpenstrasse 15, 6304 Zug, Switzerland