≡ Menu

Office 365: Connecting to Exchange Online with Remote PowerShell

As you may have heard, the Office 365 beta has started and it includes Microsoft Exchange Online, which is powered by Exchange Server 2010 SP1, and it can be fully managed via PowerShell v2 remoting. So, if you’re in the Office 365 beta, you can use the steps below to connect to Exchange Online with remote PowerShell from any machine with an internet connection. You just need PowerShell v2, which is installed by default on Windows 7 and 2008 R2. It can be downloaded and installed on down-level operating systems through Windows Update, or from here.

The process is very similar to how you would configure an implicit remoting session to an on-premise Exchange 2010 server. I blogged about how to do this here a while back. To connect to Exchange Online, use the following commands:

$cred = Get-Credential

When you execute the above command, you’ll be prompted for your Office 365 credentials. Enter your username in UPN format, such as user@domain.com. Next, create a new PSSession object using the following syntax:

$s = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell -Credential $cred -Authentication Basic -AllowRedirection

The initial remote endpoint you’ll want to use is at ps.outlook.com as shown above. The -AllowRedirection switch parameter used in this command will allow redirection to the appropriate Exchange server using different URI. You’ll see a warning message in the shell informing you of the new server URI when you execute the above command.

Next, import the cmdlets to your local shell session:

$importresults = Import-PSSession $s

This will create a PSModuleInfo object and import the cmdlets into your local session. At this point, you are ready to start working with the Exchange cmdlets, but if you take a look at the module first, you’ll notice a couple of interesting things:

$importresults.ExportedFunctions.Count

Notice that the function count for this module is only 295. This means that you only have access to a fraction of the cmdlets that you would in an on-premise Exchange 2010 environment. You can run Get-Command as shown below to page through all of the Exchange cmdlets made available through the remote session:

Get-Command -Module $importresults | Out-Host -Paging

You’ll notice as you go through the cmdlets that most of them are recipient specific cmdlets. Since this is a hosted solution, you’ll see that cmdlets like *-MailboxServer, *-ExchangeServer, etc. are not available to you.

So, that’s all there is to it. If you want to learn more about Office 365, check out the Office 365 home page, and keep an eye on the Office 365 blog for news and updates.

Update 5/19/11: Just a quick update about PowerShell execution policy…

From the cmdlet help for Import-PSSession:

To use Import-PSSession, the execution policy in the current session cannot be Restricted or AllSigned, because the module that Import-PSSession creates contains unsigned script files that are prohibited by these policies.

The default PowerShell execution policy on a machine is set to Restricted. You can change the execution policy to RemoteSigned using Set-ExecutionPolicy RemoteSigned to get the above example to work. This actually happens automatically when installing the Exchange 2010 tools on a machine, but if you are importing the cmdlets on a system without the tools, you’ll need to manually set the execution policy to a less restrictive setting (if you have not already done so). See the help file about_Execution_Policies for more details. Thanks to Terence who posted about his experience with this in the comments.

16 comments… add one

  • Sam Levine November 30, 2010, 8:27 am

    Great post. Do you have any ideas about how to connect to SharePoint in Office 365 with Remote Powershell?

  • Mike Pfeiffer November 30, 2010, 6:54 pm

    Sorry, I don’t…I haven’t worked much with SharePoint 2010, but to my knowledge it does not support fan-in remoting. So, as far as I know, the technique shown here with Exchange would not work with SharePoint.

    I don’t see anything about remote PowerShell and SharePoint in the Office 365 beta docs either. We might have to wait and see…I’ll also ask a few others and let you know if I get any more info.

  • Mike Pfeiffer April 15, 2011, 8:02 am

    Looks like it is not currently supported, but you may want to keep an eye on these threads:

    http://community.office365.com/en-us/f/154/p/1069/3411.aspx
    http://community.office365.com/en-us/f/154/p/814/2438.aspx

    Sorry for the late reply :)

  • Rae McMahan January 10, 2011, 7:39 am

    When I executed $importedresults.ExportedFunctions.Count I only got 87 back. What reasons are there for getting such a drastically smaller amount?

  • Mike Pfeiffer January 20, 2011, 7:52 am

    My account used in this is example is assigned the global administrator role. Since you’re seeing a lower count of cmdlets, your account may be assigned another role, such as the service administrator or user management administrator roles.

  • md February 4, 2011, 11:55 am

    Hello,

    Can you please tell me if I can use Powershell to set delegate for user inbox in Office 365?
    I did set folder permission, but the one that asigned the permission to my inbox and calendar folders didn’t show up in my mailbox delegate list.

    Thank you very much!

  • Thomas Lee April 15, 2011, 1:28 am

    Great article.
    One small problem in the URI spec on the call to new-session. It ‘looks’ like the /powershell is a separate line, but it’s actually part of the URI. To avoid these sorts of wrapping issues, I just use a hash table, like this:

    $Office365 = @{
    configurationName = “Microsoft.Exchange”
    connectionuri = “Https://ps.outlook.com/powershell”
    credential = $cred
    Authentication = “Basic”
    AllowRedirection = $true
    }

    Then I call the New-PsSession specifying the hash table

    $s = New-PsSession @Office365

  • Mike Pfeiffer April 15, 2011, 5:26 am

    Good point. I’ve changed the markup so it doesn’t wrap that line, but you are right, splatting sure does make it easier to read. Thanks for the comment.

  • Aleksandar Nikolic May 5, 2011, 4:32 pm

    “You just need PowerShell v2 and and Windows Remote Management (WinRM) 2.0…”

    You don’t need WinRM service running on a client machine to use PowerShell remoting. Outbound connections don’t go through the WinRM service.

  • Mike Pfeiffer May 5, 2011, 5:07 pm

    Yeah, that is not what I was getting at…sorry. What I mean is that if you are using an O/S without PowerShell v2 you can install the Windows Management Framework Core package (which includes Windows PowerShell 2.0 and WinRM 2.0) and still do this. Thanks, bad wording on my part…I updated this post.

  • Terence Rabe May 19, 2011, 2:29 am

    Hi Mike,

    Thanks for the great article. Unfortunately when I tried to import the cmdlets I got the following error.

    Import-Module : There were errors in loading the format data file:
    Microsoft.PowerShell, , C:\Users\Administrator\AppData\Local\Temp\1\tmp_814483a1-55b6-4f49-be39-5327517e30b7_broiryff.rel\tmp_814483a1-55b6-4f49-be39-5327517e30b7_broiryff.rel.format.ps1xml : File skipped because of the following validation exception: File C:\Users\Administrator\AppData\Local\Temp\1\tmp_814483a1-55b6-4f49-be39-5327517e30b7_broiryff.rel\tmp_814483a1-55b6-4f49-be39-5327517e30b7_broiryff.rel.format.ps1xml cannot be loaded because the execution of scripts is disabled on this system. Please see “get-help about_signing” for more details..

    It may be worth pointing to future readers out that on Server 2008 R2 the default execution policy is Restricted. I resolved my issue by;

    - Temporarily lifting the restiction policy with “Set-ExecutionPolicy -ExecutionPolicy unrestricted”
    - Running “$importresults = Import-PSSession $s” again
    - Reinstating the restriction policy with “Set-ExecutionPolicy -ExecutionPolicy restricted”

  • Mike Pfeiffer May 19, 2011, 10:33 am

    Thanks Terence, I updated the post to address the execution policy.

  • Dave Bole August 13, 2011, 4:59 am

    Hi Mike
    Great blog. Its exactly what I’m looking for. I’m creating mailing groups and have done the smaller ones longhand. I have used powershell a bit in sharepoint but am a bit nervous of it as it rarely works without a lot of head scratching but when it does it saves loads of time.
    I want to add lots of users to the mail groups so doing in powershell is my only option especially as I am likely to have to repeat the process.

    I used the info you provided and got logged in but the after running “$importresults = Import-PSSession $s” got the following error and can’t get any further. Can you point me in the right direction please?

    Import-Module : There were errors in loading the format data file:
    Microsoft.PowerShell, , C:\Users\Dave\AppData\Local\Temp\tmp_62c2e2ee-176c-47c1-8293-c2f29e0c0d1a_3tiimx
    e2ee-176c-47c1-8293-c2f29e0c0d1a_3tiimxfb.0m2.format.ps1xml : File skipped because of the following vali
    n: File C:\Users\Dave\AppData\Local\Temp\tmp_62c2e2ee-176c-47c1-8293-c2f29e0c0d1a_3tiimxfb.0m2\tmp_62c2e
    293-c2f29e0c0d1a_3tiimxfb.0m2.format.ps1xml cannot be loaded because the execution of scripts is disable
    m. Please see “get-help about_signing” for more details..
    At line:3 char:30
    + Import-Module <<<< -Name $name -Alias * -Function * -Prefix $prefix -DisableNameCheck
    eChecking -PassThru -ArgumentList @($session)
    + CategoryInfo : InvalidOperation: (:) [Import-Module], RuntimeException
    + FullyQualifiedErrorId : FormatXmlUpateException,Microsoft.PowerShell.Commands.ImportModuleCommand

  • Dave Bole August 13, 2011, 5:29 am

    Ignore my last posting I wasnt running powershell as an administrator and I hadnt set the execution policy as remote signed. I still couldnt get the command to work but I guess thats because I’m running from a .edu environment rather than outlook.com so I’ll try and figure that bit out now.

  • Dave Bole August 13, 2011, 2:39 pm

    OK Ive sussed it now, here’s what I did.

    Run PowerShell as Administrator

    $cred = Get-Credential

    $s = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell -Credential $cred -Authentication Basic -AllowRedirection

    Set-ExecutionPolicy -ExecutionPolicy unrestricted

    $importresults = Import-PSSession $s

    New-DistributionGroup
    add DG name

    Import-CSV FileName.csv | ForEach {Add-DistributionGroupMember -Identity “DG Name” -Member $_.Name}

    The csv file has to have the right format. In A1 type NAME and in A2copy down yor list of email user names.

  • Sathish Kumar May 6, 2013, 3:28 am

    Hello Mike,

    could you please help me out to migrate room mailboxes from exchange 2007 to office 365 using PS scripts…

    thanks in advance!

Leave a Comment