Quantcast
Channel: Practical 365
Viewing all articles
Browse latest Browse all 546

PowerShell Function to Connect to Office 365

$
0
0

Connecting to an Office 365 tenant with PowerShell is a reasonably simple task. All you need is PowerShell on your computer, which is included by default in any recent version of Windows and Windows Server.

There are three basic steps for connecting to Office 365 with PowerShell, and you’ll find these in official help documentation on TechNet as well as on many blogs. The steps are:

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

You can streamline this process by adding a custom function to your PowerShell profile. Here are two functions you can use for connecting and disconnecting from Office 365.

Function Connect-O365 {
    $URL = "https://ps.outlook.com/powershell"
    
    $Credentials = Get-Credential -Message "Enter your Office 365 admin credentials"
    $O365Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri $URL -Credential $Credentials -Authentication Basic -AllowRedirection -Name "Office 365"
    Import-PSSession $O365Session
}
Function Disconnect-O365 {
    
    Remove-PSSession -Name "Office 365"
}

After adding the functions to your PowerShell profile open a new console session and run Connect-O365 to connect.

PS C:\Users\Paul> Connect-O365

A dialog will popup asking for your Office 365 admin credentials. After a short wait the connection will be established and you can run cmdlets such as Get-Mailbox to list the mailbox users in your Office 365 tenant.

PS C:\Users\Paul> Connect-O365
WARNING: Your connection has been redirected to the following URI:
"https://pod51055psh.outlook.com/powershell-liveid?PSVersion=4.0 "
WARNING: The names of some imported commands from the module 'tmp_g5b5ehu0.amo' include unapproved verbs that might
make them less discoverable. To find the commands with unapproved verbs, run the Import-Module command again with the
Verbose parameter. For a list of approved verbs, type Get-Verb.
ModuleType Version    Name                                ExportedCommands
---------- -------    ----                                ----------------
Script     1.0        tmp_g5b5ehu0.amo                    {Add-O365AvailabilityAddressSpace, Add-O365DistributionGro...
PS C:\Users\Paul> Get-Mailbox
Name                      Alias                ServerName       ProhibitSendQuota
----                      -----                ----------       -----------------
Alan.Reid                 Alan.Reid            sixpr04mb240     49.5 GB (53,150,220,288 bytes)
Alannah.Shaw              Alannah.Shaw         sinpr04mb012     49.5 GB (53,150,220,288 bytes)
Bernadette.Hemming        Bernadette.Hemming   hkxpr04mb360     49.5 GB (53,150,220,288 bytes)
Bernadette.O'Connell      Bernadette.O'Connell sixpr04mb240     49.5 GB (53,150,220,288 bytes)
Debbie.Dalgliesh          Debbie.Dalgliesh     sinpr04mb121     49.5 GB (53,150,220,288 bytes)
Debbie.Lisa               Debbie.Lisa          sinpr04mb364     49.5 GB (53,150,220,288 bytes)
DiscoverySearchMailbox... DiscoverySearchMa... sixpr04mb015     50 GB (53,687,091,200 bytes)
Ivana.Ferrary             Ivana.Ferrary        sixpr04mb191     49.5 GB (53,150,220,288 bytes)
Jack.Hirschorn            Jack.Hirschorn       hkxpr04mb134     49.5 GB (53,150,220,288 bytes)
Kathy.Abdullahi           Kathy.Abdullahi      sinpr04mb329     49.5 GB (53,150,220,288 bytes)
Kavita.Sheikh             Kavita.Sheikh        sinpr04mb090     49.5 GB (53,150,220,288 bytes)
Melanie.Thomas            Melanie.Thomas       sixpr04mb352     49.5 GB (53,150,220,288 bytes)
Merle.Elam                Merle.Elam           sixpr04mb0382    49.5 GB (53,150,220,288 bytes)
admin                     admin                sixpr04mb048     49.5 GB (53,150,220,288 bytes)
Rowena.Khan               Rowena.Khan          hknpr04mb257     49.5 GB (53,150,220,288 bytes)
Rupinder.Zaveri           Rupinder.Zaveri      sinpr04mb267     49.5 GB (53,150,220,288 bytes)
Theresa.Bolt              Theresa.Bolt         hknpr04mb226     49.5 GB (53,150,220,288 bytes)
Tina.Miller               Tina.Miller          sixpr04mb189     49.5 GB (53,150,220,288 bytes)

To disconnect the session use the Disconnect-O365 function.

PS C:\Users\Paul> Disconnect-O365

As an additional tip, if you’re running a hybrid configuration and using an Exchange Management Shell session to connect to Office 365 you may run into issues due to the same cmdlets being available in both your on-premises Exchange and Exchange Online. In that situation you can use Jeff’s tip of adding a prefix to the Import-PSSession command.

For example:

Function Connect-O365 {
    $URL = "https://ps.outlook.com/powershell"
    
    $Credentials = Get-Credential -Message "Enter your Office 365 admin credentials"
    $O365Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri $URL -Credential $Credentials -Authentication Basic -AllowRedirection
    Import-PSSession $O365Session -Prefix O365
}

You might prefer to use a prefix of “Online” or “Cloud” instead. Really it is up to you. But when you do this the prefix is added to the cmdlets for your Office 365 session, so Get-Mailbox becomes Get-O365Mailbox instead.

PS C:\Users\Paul> Get-O365Mailbox
Name                      Alias                ServerName       ProhibitSendQuota
----                      -----                ----------       -----------------
Alan.Reid                 Alan.Reid            sixpr04mb240     49.5 GB (53,150,220,288 bytes)
Alannah.Shaw              Alannah.Shaw         sinpr04mb012     49.5 GB (53,150,220,288 bytes)
Bernadette.Hemming        Bernadette.Hemming   hkxpr04mb360     49.5 GB (53,150,220,288 bytes)
Bernadette.O'Connell      Bernadette.O'Connell sixpr04mb240     49.5 GB (53,150,220,288 bytes)
Debbie.Dalgliesh          Debbie.Dalgliesh     sinpr04mb121     49.5 GB (53,150,220,288 bytes)
Debbie.Lisa               Debbie.Lisa          sinpr04mb364     49.5 GB (53,150,220,288 bytes)
DiscoverySearchMailbox... DiscoverySearchMa... sixpr04mb015     50 GB (53,687,091,200 bytes)
Ivana.Ferrary             Ivana.Ferrary        sixpr04mb191     49.5 GB (53,150,220,288 bytes)
Jack.Hirschorn            Jack.Hirschorn       hkxpr04mb134     49.5 GB (53,150,220,288 bytes)
Kathy.Abdullahi           Kathy.Abdullahi      sinpr04mb329     49.5 GB (53,150,220,288 bytes)
Kavita.Sheikh             Kavita.Sheikh        sinpr04mb090     49.5 GB (53,150,220,288 bytes)
Melanie.Thomas            Melanie.Thomas       sixpr04mb352     49.5 GB (53,150,220,288 bytes)
Merle.Elam                Merle.Elam           sixpr04mb0382    49.5 GB (53,150,220,288 bytes)
admin                     admin                sixpr04mb048     49.5 GB (53,150,220,288 bytes)
Rowena.Khan               Rowena.Khan          hknpr04mb257     49.5 GB (53,150,220,288 bytes)
Rupinder.Zaveri           Rupinder.Zaveri      sinpr04mb267     49.5 GB (53,150,220,288 bytes)
Theresa.Bolt              Theresa.Bolt         hknpr04mb226     49.5 GB (53,150,220,288 bytes)
Tina.Miller               Tina.Miller          sixpr04mb189     49.5 GB (53,150,220,288 bytes)

This article PowerShell Function to Connect to Office 365 is © 2014 ExchangeServerPro.com

Get more Exchange Server tips at ExchangeServerPro.com

     

Viewing all articles
Browse latest Browse all 546

Trending Articles