Connect-O365Exchange.ps1


Description

Purpose

Connect-O365Exchange - A function to connect to Office 365 Exchange Online using Modern Authentication.

Detailed Description

Connect-O365Exchange - A function to connect to Office 365 Exchange Online using Modern Authentication. This function will import the ExchangeOnlineManagement module and connect to Exchange Online using the credentials of the user named in UserName. If UserName is left blank it will try to use the default account for the powershell session, using the ‘$env:USERNAME’ environment variable.

Back to Top

Usage

Example 1

Connect-O365Exchange -UserName "lukeleigh.admin"

Connects using the account named in UserName

Example 2

Connect-O365Exchange

Connects using the environment variable $Env:USERNAME

Example 3

ex365 -UserName "lukeleigh"

Using the command alias, this command connects using the account named in UserName

Back to Top

Notes

Author: Luke Leigh Website: https://scripts.lukeleigh.com/ LinkedIn: https://www.linkedin.com/in/lukeleigh/ GitHub: https://github.com/BanterBoy/ GitHubGist: https://gist.github.com/BanterBoy

Back to Top


Script

Function Connect-O365Exchange {

    <#

    .SYNOPSIS
    Connect-O365Exchange - A function to connect to Office 365 Exchange Online using Modern Authentication.
	
	.DESCRIPTION
    Connect-O365Exchange - A function to connect to Office 365 Exchange Online using Modern Authentication. This function will import the ExchangeOnlineManagement module and connect to Exchange Online using the credentials of the user named in UserName. If UserName is left blank it will try to use the default account for the powershell session, using the '$env:USERNAME' environment variable.
	
	.PARAMETER UserName
    [string]UserName - Enter a username with permissions to Office 365. If left blank it will try to use the default account for the powershell session, using the '$env:USERNAME' environment variable.
	
	.EXAMPLE
    Connect-O365Exchange -UserName "lukeleigh.admin"

    Connects using the account named in UserName
	
	.EXAMPLE
    Connect-O365Exchange

    Connects using the environment variable $Env:USERNAME
    
	.EXAMPLE
    ex365 -UserName "lukeleigh"

    Using the command alias, this command connects using the account named in UserName
	
	.OUTPUTS
    No output returned.
	
	.NOTES
    Author:     Luke Leigh
    Website:    https://scripts.lukeleigh.com/
    LinkedIn:   https://www.linkedin.com/in/lukeleigh/
    GitHub:     https://github.com/BanterBoy/
    GitHubGist: https://gist.github.com/BanterBoy
	
	.INPUTS
    You can pipe objects to these perameters.
		
    - UserName [string] - Enter a username with permissions to Office 365. If left blank it will try to use the default account for the powershell session, using the '$env:USERNAME' environment variable.
	
	.LINK
    https://scripts.lukeleigh.com
    Import-Module
    Connect-ExchangeOnline

    #>
	
    [CmdletBinding(DefaultParameterSetName = 'Default',
        ConfirmImpact = 'Medium',
        SupportsShouldProcess = $true,
        HelpUri = 'http://scripts.lukeleigh.com/')]
    [OutputType([string], ParameterSetName = 'Default')]

    param
    (
        [Parameter(ParameterSetName = 'Default',
            Mandatory = $false,
            ValueFromPipeline = $true,
            ValueFromPipelineByPropertyName = $true,
            HelpMessage = 'Enter a login/SamAccountName with permissions to Office 365 e.g. "lukeleigh.admin". If left blank it will try to use the default account for the powershell session, using the env:USERNAME environment variable.')]
        [ValidateNotNullOrEmpty()]
        [string]$UserName = $env:USERNAME
    )
    
    begin {

    }
    
    process {
        if ($PSCmdlet.ShouldProcess($UserName, "Connecting to Exchange Online as")) {

            Import-Module ExchangeOnlineManagement
            Connect-ExchangeOnline -UserPrincipalName (Get-ADUser -Identity $UserName).UserPrincipalName -ShowBanner:$false

        }
    }

    end {

    }
}

Back to Top

Download

Please feel free to copy parts of the script or if you would like to download the entire script, simply click the download button. You can download the complete repository in a zip file by clicking the Download link in the menu bar on the left hand side of the page.


Report Issues

You can report an issue or contribute to this site on GitHub. Simply click the button below and add any relevant notes. I will attempt to respond to all issues as soon as possible.

Issue


Back to Top