{"id":3163,"date":"2019-02-12T09:04:41","date_gmt":"2019-02-12T07:04:41","guid":{"rendered":"https:\/\/www.msb365.blog\/?p=3163"},"modified":"2023-06-23T13:07:29","modified_gmt":"2023-06-23T11:07:29","slug":"powershell-function","status":"publish","type":"post","link":"https:\/\/www.msb365.blog\/?p=3163","title":{"rendered":"Powershell function for Exchange online"},"content":{"rendered":"<p>Working as a consultant for multiple customers forces us to connect multiple times a day to different Exchange Online management shell&#8217;s. Everyone has his own way to save the connection command for the Exchange Online PowerShell. I had the command always saved in a ready to use Notepad document.<\/p>\n<p>However, since I am always using the same devices for work, we created a small PowerShell function which makes it easier and faster to connect to customers Exchange Online environments.<\/p>\n<p>&nbsp;<\/p>\n<h2>The Function<\/h2>\n<pre class=\"lang:ps decode:true \">function Connect-ExchangeOnline\r\n{\r\n\t&lt;#\r\n.NOTES\r\n===========================================================================\r\nCreated on: 08.02.2019 14:43\r\nCreated by: Dominic Manning | Drago Petrovic\r\nWebsite: https:\/\/www.msb365.blog\r\nName: Connect-ExchangeOnline\r\n===========================================================================\r\n.Synopsis\r\n   Connect-ExchangeOnline is a PowerShell function to ease the connection to Exchange Online\r\n.DESCRIPTION\r\n   Function to connect to Exchange Online services. \r\n\tIf the function was executed before in the same PoSH session you can re-use the credentials provided before.\r\n\tYou can also specify a specific O365 tenant domain to connect to if you provide the\r\n\tparameter \"Tenant\"\r\n.EXAMPLE\r\n   Connect-ExchangeOnline -Tenant contoso.onmicrosoft.com\r\n\tWill connect to the tenant domain contoso.onmicrosoft.com\r\n.EXAMPLE\r\n   Connect-ExchangeOnline\r\n\tWill connect to the default tenant domain of your specified credentials.\r\n.EXAMPLE\r\n\tDisconnect-ExchangeOnline\r\n\tDisconnects the session.\r\n\r\n#&gt;\r\n\t\r\n\tparam\r\n\t(\r\n\t\t[Parameter(Mandatory = $false)]\r\n\t\t[String]$Tenant\r\n\t)\r\n\t# First check if we have credentials from a former execution \r\n\t# and ask if we want to use those credentials again to connect\r\n\tif ($O365creds -ne $null)\r\n\t{\r\n\t\t$yn = Read-Host \"Found O365 credentials for $($O365creds.username). Use those?[Y\/N] Default is [Y]\"\r\n\t\tswitch ($yn)\r\n\t\t{\r\n\t\t\t\"y\" {\r\n\t\t\t\t\"Using credentials for $($O365creds.username)\"\r\n\t\t\t\t$oldcreds = $true\r\n\t\t\t}\r\n\t\t\t\"N\" {\r\n\t\t\t\t$oldcreds = $false\r\n\t\t\t}\r\n\t\t\tdefault\r\n\t\t\t{\r\n\t\t\t\t\"Using credentials for $($O365creds.username)\"\r\n\t\t\t\t$oldcreds = $true\r\n\t\t\t}\r\n\t\t}\r\n\t}\r\n\t# Ask for credentials if no old credentials were found, or by user input specified not to use them\r\n\tif ($O365creds -eq $null -or $oldcreds -eq $false)\r\n\t{\r\n\t\t$global:O365creds = (Get-Credential -Message \"Enter your O365 \/ Exchange Online credentials\")\r\n\t\tif ($O365creds -eq \"\" -or $O365creds -eq $null)\r\n\t\t{\r\n\t\t\t#Exit if no credentials are provided\r\n\t\t\t\"No credentials provided. Exiting.\"\r\n\t\t\treturn\r\n\t\t}\r\n\t}\r\n\t\r\n\ttry\r\n\t{\r\n\t\t\"Creating session...\"\r\n\t\t#If a tenant domain name was specified use that name in the connection URI\r\n\t\tif ($tenant -ne \"\")\r\n\t\t{\r\n\t\t\t#Create the session with domain name in the URI\r\n\t\t\t$global:o365session = New-PSSession -ConfigurationName Microsoft.Exchange -uri https:\/\/ps.outlook.com\/powershell-liveid?DelegatedOrg=$tenant -Credential $O365Creds -Authentication Basic -AllowRedirection -Name \"Exchange Online\" -ErrorAction Stop\r\n\t\t}\r\n\t\telse\r\n\t\t{\r\n\t\t\t#Create the session\r\n\t\t\t$global:o365session = New-PSSession -ConfigurationName Microsoft.Exchange -uri https:\/\/ps.outlook.com\/powershell-liveid? -Credential $O365Creds -Authentication Basic -AllowRedirection -Name \"Exchange Online\" -ErrorAction Stop\r\n\t\t}\r\n\t\t\r\n\t}\r\n\tcatch\r\n\t{\r\n\t\tWrite-Error \"Could not connect to O365\"\r\n\t\t$_.Exception.Message\r\n\t\t\r\n\t}\r\n\t\"Importing session\"\r\n\t#Import the session through import module \"-Global\" so the cmdlets are globally available if run as module.\r\n\tImport-Module(Import-PSSession $o365session -ErrorAction Stop) -Global -ErrorAction Stop\r\n\tcls\r\n\tWrite-Host \"Connected to Exchange Online. Sponsored by MSB365.blog\" -ForegroundColor Green\r\n}\r\n\r\nfunction Disconnect-ExchangeOnline\r\n{\r\n\tif (Get-PSSession -Name \"Exchange Online\")\r\n\t{\r\n\t\tRemove-PSSession -Name \"Exchange Online\"\r\n\t\t\"Session disconnected\"\r\n\t}\r\n}<\/pre>\n<h2>How to use the Function<\/h2>\n<p>Copy the code into your PowerShell, or PowerShell\u00a0ISE session and run it. Then type <em>Connect-ExchangeOnline<\/em> and have fun with it.<\/p>\n<p>If we want to make the function permanently available so it&#8217;s is there every time we start PowerShell, we have to create a <strong>folder<\/strong> in <strong>C:\\Program Files\\WindowsPowerShell\\Modules<\/strong>. This folder we name\u00a0<em>Connect-ExchangeOnline<\/em>. After that we save the code (our function) as a .psm1 file in that folder.<\/p>\n<p>To see an example, have a look on the picture below:<\/p>\n<p><img fetchpriority=\"high\" decoding=\"async\" class=\"alignnone size-full wp-image-3249\" src=\"https:\/\/msb365.abstergo.ch\/wp-content\/uploads\/2019\/02\/c-exo.png\" alt=\"\" width=\"805\" height=\"209\" srcset=\"https:\/\/msb365.abstergo.ch\/wp-content\/uploads\/2019\/02\/c-exo.png 805w, https:\/\/msb365.abstergo.ch\/wp-content\/uploads\/2019\/02\/c-exo-300x78.png 300w, https:\/\/msb365.abstergo.ch\/wp-content\/uploads\/2019\/02\/c-exo-768x199.png 768w, https:\/\/msb365.abstergo.ch\/wp-content\/uploads\/2019\/02\/c-exo-600x156.png 600w\" sizes=\"(max-width: 805px) 100vw, 805px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<h2>Connect to Exchange online<\/h2>\n<p>I want to show you first what we are going to achieve.<\/p>\n<p>After starting the default Windows PowerShell cmdlet, we simply hit the command <em>Connect-ExchangeOnline<\/em>\u00a0and we get prompted to the logon windows for our credentials. Depending on which credentials we are using here, we are going to be connected to our environment.<\/p>\n<p><img decoding=\"async\" class=\"alignnone size-full wp-image-3189\" src=\"https:\/\/msb365.abstergo.ch\/wp-content\/uploads\/2019\/02\/Connect-ExchangeOnline2.png\" alt=\"\" width=\"616\" height=\"529\" srcset=\"https:\/\/msb365.abstergo.ch\/wp-content\/uploads\/2019\/02\/Connect-ExchangeOnline2.png 616w, https:\/\/msb365.abstergo.ch\/wp-content\/uploads\/2019\/02\/Connect-ExchangeOnline2-300x258.png 300w, https:\/\/msb365.abstergo.ch\/wp-content\/uploads\/2019\/02\/Connect-ExchangeOnline2-600x515.png 600w\" sizes=\"(max-width: 616px) 100vw, 616px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>If we are managing multiple Exchange online instances (As CSP for example) we can run the following command:<\/p>\n<pre class=\"\">Connect-ExchangeOnline -Tenant contoso.onmicrosoft.com<\/pre>\n<p><img decoding=\"async\" class=\"alignnone size-full wp-image-3250\" src=\"https:\/\/msb365.abstergo.ch\/wp-content\/uploads\/2019\/02\/c-exo-1.png\" alt=\"\" width=\"638\" height=\"388\" srcset=\"https:\/\/msb365.abstergo.ch\/wp-content\/uploads\/2019\/02\/c-exo-1.png 638w, https:\/\/msb365.abstergo.ch\/wp-content\/uploads\/2019\/02\/c-exo-1-300x182.png 300w, https:\/\/msb365.abstergo.ch\/wp-content\/uploads\/2019\/02\/c-exo-1-600x365.png 600w\" sizes=\"(max-width: 638px) 100vw, 638px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<h2>Get the function<\/h2>\n<p>This function can easily be copied out from this article or downloaded from the Microsoft Technet by following the link below:<\/p>\n<p><a href=\"https:\/\/gallery.technet.microsoft.com\/Powershell-function-for-b8eddf32\" target=\"_blank\" rel=\"noopener\">https:\/\/gallery.technet.microsoft.com\/Powershell-function-for-b8eddf32<\/a><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>Photo by\u00a0<a href=\"https:\/\/unsplash.com\/photos\/401OD83Ke6o?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText\">Malcolm Lightbody<\/a>\u00a0on\u00a0<a href=\"https:\/\/unsplash.com\/search\/photos\/gear?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText\">Unsplash<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Working as a consultant for multiple customers forces us to connect multiple times a day to different Exchange Online management shell&#8217;s. Everyone has his own way to save the connection command for the Exchange Online PowerShell. I had the command always saved in a ready to use Notepad document. However, since I am always using [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":3174,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_crdt_document":"","om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_uf_show_specific_survey":0,"_uf_disable_surveys":false,"footnotes":""},"categories":[1923,2,3],"tags":[],"class_list":["post-3163","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-microsoft-365","category-exchange","category-powershell"],"post_mailing_queue_ids":[],"_links":{"self":[{"href":"https:\/\/www.msb365.blog\/index.php?rest_route=\/wp\/v2\/posts\/3163","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.msb365.blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.msb365.blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.msb365.blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.msb365.blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3163"}],"version-history":[{"count":37,"href":"https:\/\/www.msb365.blog\/index.php?rest_route=\/wp\/v2\/posts\/3163\/revisions"}],"predecessor-version":[{"id":5191,"href":"https:\/\/www.msb365.blog\/index.php?rest_route=\/wp\/v2\/posts\/3163\/revisions\/5191"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.msb365.blog\/index.php?rest_route=\/wp\/v2\/media\/3174"}],"wp:attachment":[{"href":"https:\/\/www.msb365.blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3163"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.msb365.blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3163"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.msb365.blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3163"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}