Art of Automation: Leveraging PowerShell Modules and Starting with Office 365 PowerShell
Recently I published an introductory article about how PowerShell can help transform your business. The article provided a primer to start learning the PowerShell language. You can read the article here:
In this article I am going to talk a little bit about how to find what commands you can use in PowerShell as well as extending the functionality by importing PowerShell modules. Which you will have to know how to do if you want to leverage PowerShell in managing your customers Office 365 tenants.
PowerShell Modules Overview
One of the most valuable PowerShell cmdlets you will want to learn to use is Get-Command. This will show a list of all the cmdlets available to you in your PowerShell session. Some of them may surprise you (like dir) and we will talk about those in future post on aliases. As you can see there are quite a few cmdlets. Before we starting digging into the cmdlets you first need to know that is not all the possible cmdlets you may need to accomplish your task. The listing of cmdlets you get from Get-Command does not show you all of the cmdlets on your system, only the currently loaded ones. Based on what roles or other services you may have loaded on your system, PowerShell may have not loaded the corresponding cmdlets which support those particular services or functions. You will need to not only learn if you have cmdlets available, but also how to load them, so keep reading. Also many software manufactures(outside of Microsoft applications) include PowerShell cmdlets in their toolsets, and these cmdlets will also need to be loaded to be used. To see the cmdlets currently available to you run the following cmdlet:
Get-Module –ListAvailable (you will something similar to the screen shot)
Depending on the OS that you are running you may see different modules. The list of modules you see will also vary based on what roles and functions you have installed on the server. To load the cmdlets for a particular module you will need to use the Import-Module cmdlet. When you import the module the cmdlets are temporarily loaded in your current PowerShell session. For example to load the AppLocker module you would run the following command:
Of special note, in PowerShell 3.0 we introduced a new function called auto-loading which allows you to run cmdlets without having to use the Import-Module cmdlet. However, when writing PowerShell scripts it is generally a good idea to use the Import-Module cmdlet to load the modules you will need to accomplish your task. If you want to disable auto-loading take a look at this article: PowerTip: Turn Off PowerShell Module Autoload
While Get-Command is great at listing all the cmdlets you can run, you will most likely want to view just the cmdlets for a particular module, which you can do by running the following command, which list the cmdlets for the AppLocker module.
Get-Command –Module AppLocker
Azure Active Directory Module (formerly Office 365 Module)
Now with that all said , lets start managing Office 365 with PowerShell! As a FYI the Office 365 module has been renamed to the Azure Active Directory Module. Do not worry you can do more than just manage Azure AD. Also important to note you must be a tenant admin on your Office 365 tenant to run the cmdlets.
To begin working with Office 365 there are a few steps needed to get you going:
- Install the Microsoft Online Service Sign-in Assistant for IT Professionals RTW. There are 32 and 64 bit versions.
- Install the Azure AD Module for Windows PowerShell:
- Import the Azure AD Module, btw under the covers the modules actual name is msonline. This is important when you want to use the module name in scripts.
Ed Wilson wrote a great post detailing out the above. It is a great read located here: Getting Started With Office 365 and PowerShell
Get-Command -Module msonline
The screenshot will look similar to your results, there are several cmdlets and you can learn more about them here: Manage Azure AD using Windows PowerShell
In my next Art of Automation post, I will talk about logging into the Office 365 tenant and performing some PowerShell tasks. If you want to get started sooner rather than later take a look at Ed Wilson’s post mentioned above.
Here are some links to even more resources to start learning the basic of the language. I highly recommend the Hey, Scripting Guy! Blog. It is a great blog with tons of great information!
- PowerShell Not Your Father's Command Line: 31 Days of PowerShell – This is a series I did in May 2011, while the series is older, a lot of the main concepts are the same. Enjoy!
- Windows PowerShell Core Modules
- Hey, Scripting Guy! Blog
- Script Center Script Repository
- Learn Windows PowerShell Scripting
Hopefully you have enjoyed this post and hopefully has given you a way to start managing your customers Office 365 tenant. Again you can help contribute to these topics in Art of Automation. So if there is a topic on PowerShell you would like to learn more about to help your business then comment on this post. I look forward to your comments and suggestions and hearing from you.
In the meantime, Get-PowerShelling!
Sr. Partner Technology Strategist