Create a function to Connect to Office 365 and add to your powershell profile

Office 365

Each time you want to run powershell commands to manage office 365 you need to load the relevant modules for each corresponding session (Office 365 admin center, SharePoint Online, Exchange Online, Skype for Business Online, and the Security & Compliance Center).

Instead of running the relevant cmdlets each time create a function to do the job and add it your powershell profile.

Before we begin, there are a few requirements

Powershell Profile

To check if you have a profile or modify your powershell profile see my post

The function(s)

Personally, in my profile I have several functions to load the various Office 365 modules. I do not necessarily want to load them all. Add this code to your profile.

These functions load the modules and also prompt you what to type to load the modules.

save and start powershell and you should see the prompts that will load the modules:

If you type Connect-O365, you’ll get the prompt to enter your credentials.

You could also include your credentials in the functions:

I have seen another way of implementing this which looks interesting but have not tried it.

In your profile add the following code:

This will automatically load scripts that are placed in”C:\AutoLoadScripts” (could be interesting for other things).

Place this script for Office 365 in C:\AutoloadScripts or wherever you specfied in the profile. This script can be downloaded from Microsoft Technet Script Gallery or see  https://syscloudpro.com/2016/12/18/all-in-one-office-365-powershell-connect/

 

 

No Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Office 365
Import Contacts to Office 365 Global Address List

Office 365 doesn’t offer you a way to easily import a list of contacts into your company’s Global Address List (GAL). But it can be done quite easily with Powershell and a csv file of the contacts to import. First of all the csv file. There are some mandatory fields …

Office 365
Save Sent Items in Shared Mailbox

The default behavior for shared mailboxes is that the sent mail will go to the sent items folder of the senders mailbox. This means other users with shared access cannot see what has been sent from this mailbox. We can change this so the sent items are saved to the …

Office 365
Create a Dynamic Distribution Group of All Users in Office 365

Launch Powershell as an Administrator and connect to Office 365 Run New-DynamicDistributionGroup -IncludedRecipients MailboxUsers -Name “AllUsers” That’s it – Dynamic Distribution Group created. To check the membership of the group…… run $AllUsers = Get-DynamicDistributionGroup “AllUsers” Get-Recipient -RecipientPreviewFilter $AllUsers.RecipientFilter | FT DisplayName, Alias, PrimarySMTPAddress   Related