PowerShell is based on the .NET framework, and is aimed at simplifying task automation and configuration management. It's particularly useful for SharePoint and Office 365 admins trying to make their lives easier.
Why use PowerShell?
The classic Windows command line utility (cmd.exe) has long been the tool many IT admins used to execute basic commands. But it's very limited, and to achieve extended functionality, a developer has to write a console application. Even then, more powerful scripting capabilities just aren’t available. With PowerShell, this changes! PowerShell provides the following key features, invaluable to many developers or administrators:
- Automate tasks with scripts
- PowerShell can interact with numbers of different technologies
- Create your own commands (cmdlets) and share with others
- Powerful Graphical editor support for writing PowerShell scripts called as PowerShell ISE
- Implement consistency of the task management across environment
- Powerful community support and good documentation
SharePoint and PowerShell
PowerShell is really handy for managing and maintaining a SharePoint Farm, especially for automating repetitive tasks. It comes as default on Windows Server. To manage SharePoint, there's an additional shell - the SharePoint Management shell - which comes pre-loaded with a set of SharePoint libraries ready to perform specific common activities.
Microsoft also makes a great tool available to generate PowerShell commands based on the verbs and noun actions of your required task. You can easily generate a PowerShell command by setting the appropriate property, and with the simple ‘Copy to clipboard’ action, you can use that command in your PowerShell. Pretty neat!
Connect to Office 365 using PowerShell
Managing a SharePoint ‘On Premise’ farm is fairly easy using PowerShell, but when it comes to SharePoint Online/Office 365, things have not always been so simple. But with the ‘Microsoft Online Service Sign-in Assistant for IT Professionals RTW’ you can now manage Office 365 using PowerShell remotely from any machine.
You need to download and install the Microsoft Online Service Sign-in Assistant for IT Professionals RTW on your machine. Note: Download this as it’s a 64 bit version and Microsoft isn't going to provide support for 32 bit version in future. Once it's installed, you can download and install the Windows Azure Active Directory Module for PowerShell.
Once installation is completed, simply launch the Windows Azure Active Directory Module for PowerShell. Now it's time to connect to Office 365. Simply execute the following:
Then enter your credentials for Office 365. Upon successful authentication credentials are stored in the $officecredentials variable.
Tips & Tricks for Office 365 using PowerShell
There's a variety of commands available for managing Office 365 using the PowerShell. Below are some key tips.
Tip #1: Get a list of users and create a new user
In order to get a list of Office 365 users, we can use the command below. It'll retrieve all users with a valid license in Office 365 along with the DisplayName, City and Department. You can also retrieve other properties as required by appending the select statement.
In order to create a new user, we can use the New-MsolUser command to create a new user as below:
With these commands, we can create a PowerShell script to create multiple users in bulk. This is quite helpful when you want to migrate from an existing SharePoint environment to Office 365. I can create a list of user display names in an Excel file. A PowerShell script can then read the file and create the new users automatically!
Tip #2: Managing Office 365 Passwords with PowerShell
In the previous tip, we discussed creating a new user, but what about their password? Using the above as an example, Robert Wai will receive an email with the temporary password to login to the site for the first time. After which he'll be forced to change it to a new password.
As an Office 365 admin, you can also change the password for other accounts using the Set-MsolUserPassword command. In the below command, we have set the ForceChangepassword to false (0) so that when the user logins for the first time, he won’t be forced to change the password.
Tip #3: Managing Office 365 Groups
We can manage the various Groups in Office 365 with PowerShell. In order to retrieve the list of all the Groups in the Office 365 we can use the command Get-MsolGroup.
In order to add the users to the Group, we can use the Add-MsolGroupMember command:
GroupObjectId – Group object id of the group created. You can get it from the Get-MsolGroup command.
GroupMemberObejctId – User object id.
Tip #4: Managing Office 365 Sites
Once we have users and a Group in place, we can go ahead and create a SharePoint Site Collection using PowerShell:
Automation saves time and money
As everyone knows, often time is indeed money. Automation of tasks using PowerShell reduces lots of the effort required by the administrators and developers to manage a myriad of functions in Office 365. Additionally, it reduces risk, human error, improves the reliability of services and enables scaling across environments - points any organization would wish for. Happy learning!