PowerShell Profiles

Often times, I see fellow admins doing a web search in order to copy\paste the necessary cmdlets from TechNet to connect to various Office 365 services like Skype for Business Online or Exchange Online. While that works fine, it’s a waste of time. The power of PowerShell should be leveraged, and using PowerShell profiles is the way to do that in this case. There is a great article from the “Hey, Scripting Guy!” blog here that goes over the many profile options you have, so I won’t belabor the topic on this blog.

I want to share how I leverage profiles to make connecting to Office 365 services easier and quicker. in this case, I add profiles to “Current User, Current Host – console” profile and the “Current User, Current Host, ISE” profile. To see for yourself how this stuff works, open up PowerShell ISE and also open up a regular PowerShell console. Type the variable:

In the PowerShell console, you’ll get this:

Notice the name of the file is “Microsoft.PowerShell_profile.ps1”

In the PowerShell ISE window, you’ll get this:

Notice here that the name of the file is “Microsoft.PowerShellISE_profile.ps1”

Now, if you navigate to the PowerShell directory (and you’ve never touched profiles before) you might notice that those files aren’t there. What’s with that??? Well, you have to create them!! That’s easy – just navigate to the folder and create the files. Again, read the article mentioned above for more details about this. There is great info there. You also need to make sure you have all the necessary PS modules installed – Azure AD, Skype for Business Online, SharePoint Online, and the Sign-In Assistant. MFA can also play a factor so make sure to read up on that to see how it affects how you connect. For example, EXO has a new MFA-enabled PowerShell console. Anyway, now to the good stuff…

To connect to Exchange Online, use the following function in the file(s):

Here’s the code to disconnect from EXO (more important than you think – it’s important to clean up your PS sessions when you can!):

Connect to Skype for Business Online:

Disconnect from Skype for Business Online:

Connect to SharePoint Online (you’ll have to replace the proper information here for the $GAUPN and $org variables):

Now you can copy and paste all of those functions into your profile files and those functions will be available for AutoComplete as needed:

Obviously, feel free to tweak the code as needed to make it work better for you. I hope this saves you some time down the road when you need to get your PowerShell on and maybe find new and cool ways to make your admin life easier through PowerShell!

 

Advertisements

Leave a Reply

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