Office 365

Getting Office 365 PowerShell Automation: Getting Started

In this post I am going to cover two things:

  1. Why should you consider Office 365 PowerShell
  2. Getting started with Visual Studio Code and PowerShell

Why Office 365 PowerShell? If you are an Office 365 user, consumer whatever you want to call it, this is not for you. PowerShell is used by IT Pros and Administrators to script certain actions to make them schedulable and repeatable. Microsoft has a long history with PowerShell and there are no indications that is ever going to change. When you look at Microsoft products there are a lot of functional holes and in some cases features that haven’t made it in the user interface (yet). However, these features and these functional holes are always covered by PowerShell, if you can’t do something through the interface, I can guarantee you that Microsoft does support the PowerShell for it. So if you want to configure certain settings and features, you will need Office 365 PowerShell. The second reason is for the ability to schedule and automation. The user interface cannot be automated. PowerShell and scripts are designed with that in mind. You can create a script and let an executor or scheduled job run it whenever you want it to run.

Getting started with Visual Studio Code. Even though I am a huge fan of PowerShell ISE and what it can do, we all have to evolve and embrace the toolset that allows us to be more productive. Visual Studio Code allows you to code in different languages and integrate with source safe systems like GitHub, Visual Studio Services, etc. Since it took me longer that I thought to get the integration with Visual Studio Services going I am adding a step by step guide on how to get started with VS Code and VS Services. If couldn’t care less about storing your script in a safe location, you can skip most steps. I included a non -integration step-by-step plan.

Prerequisites (my setup if for Windows 64 Bit): 

  1. Install Visual Studio Code:
  2. Install GitHub:
  3. Login to your account on
  4. Create a new account if you are starting empty [otherwise select your account and create a new team project].
    1. Give it a name
    2. Manage code using Git
  5. I like to initialize my repository with a README. It forces me to add documentation and at the same time the repository is created without any problems. Click Initialize
  6. Click on Code > Files and click on the Clone button. Copy the link in the HTTPS field. You will need this to set up your workspace.

Visual Studio Code

  1. Time to install some extensions. Click on the bottom icon in the left menu (or use the shortcut CTRL + SHIFT + X).
  2. Install the following Extensions
    1. PowerShell
    2. Visual Studio Team Services

GitHub Repository

  1. Open GitHub Bash
  2. We are going to configure our email address to be able to commit and push into Visual Studio Team Services. Type: git config –global “<email address>”
  3. Now we are going to clone our repository. Use the following command: git clone <Clone URL you copied> <Location of the local repository>. The location needs to be formatted like this c:/workspaces/AutomateO365 
  4. Now you will see in the workspace directory a .git folder and your readme document you used to initialize the repository.

Visual Studio Code

  1. Open Visual Studio Code
  2. Press F1
  3. Type: Team:Signin
  4. I used the Authenticate and get an access token automatically. 
  5. If all is successful you should see a connection to your Visual Studio Services in your status bar. ConnectiontoVS
  6. Select File > Open Folder and open the root folder of your local repository you created.
  7. Select the file Explorer (first option in the left menu or use CTRL + SHIFT  + E) to see the file in the project.
  8. Use the buttons +Folder and +File to create new files and folders where necessary.
  9. Let’s add a file and call it ConnectToOffice365.ps1
  10. Add the following Code. The first line is going to install the module for Office 365 (you need admin credentials for this), you only need to do this once. The second line with import the module once installed and the third line will connect to Office 365.Install-Module MSOnline -Scope CurrentUser
    Import-Module MSOnlineConnect-MsolService
  11. Run the script by clicking F5. If everything is setup correctly you will get a login screen. Provide your Office 365 credentials and it will connect to the PowerShell Interface of Office 365
  12. Save the file.
  13. Since we are using Visual Studio Services, the file should appear in green, indicating it is a new file. A modified file will appear orange. You will also see a U for untracked and a M for modified.
  14. Go the Source Control section (third option in the left menu or CTRL + SHIFT + G)
  15. If you are happy with the files and ready to send to a safe location, you can commit them by clicking on the V next to the source control. Provide a commit message so you know what happened to the files.  To push these changes back to the Cloud Service, you need to push them as well, you can do that by clicking on the ellipsis (…) and select Push. 

Now our environment is ready to start creating some Office 365 Automation. If we create something interesting, at least our environment is setup to save and store it in a safe location.

If you don’t care about the source safe, just do the following steps and you are golden.

  1. Install Visual Studio Code:
  2. Time to install some extensions. Click on the bottom icon in the left menu or use the shortcut CTRL + SHIFT + X.
  3. Install the PowerShell Extension.
  4. Create a new document (make sure the extension is .ps1) and run the followingInstall-Module MSOnline -Scope CurrentUser
    Import-Module MSOnlineConnect-MsolService
  5. Run the script by clicking F5. If everything is setup correctly you will get a login screen. Provide your Office 365 credentials and it will connect to the PowerShell Interface of Office 365
  6. Save the file.

That is it.









  1. Super useful guide. I think there is a step missing in the prerequisites between #2 and #3. Once you install GitHub Desktop and sign in, it will ask you to add a repository. I guess the advice here is to cancel the creation of the local repository and then rather than use Git Hub Bash you can use the ‘Clone repository’ feature in Git Hub desktop?


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

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

%d bloggers like this: