SharePoint Online Management Shell – Part 2: Setup the PowerShell session

2017-12-11T10:33:55+00:00 July 29th, 2015|PowerShell, Sharepoint|

Introduction

Welcome to part two of my SharePoint Online Management Shell series. Today is by far the simplest and quickest stage where I will walk you through setting up your PowerShell session ready for SharePoint Online operations with PowerShell.

Pre-Requisites

  • You must have the SharePoint Online Management Shell for Windows PowerShell installed

Process

First, open PowerShell and then execute the following cmdlet:

Import-Module Microsoft.Online.SharePoint.PowerShell -DisableNameChecking;

Done!

Wait? You want some graceful handing is the Import-Module fails? Ok, well let’s start as we mean to go on then… Let’s evaluate if the Module is loaded first, then if it isn’t attempt to load it. If the loading fails, then format the error for outputting to the session:

 if ((Get-Module Microsoft.Online.SharePoint.PowerShell).Count -eq 0) {
  Write-Host "Attempting to import module... " -NoNewLine;
  try {
    Import-Module Microsoft.Online.SharePoint.PowerShell -DisableNameChecking -ErrorAction Stop;
    Write-Host -ForegroundColor Green "Import completed";
  }
  catch {
    Write-Host -ForegroundColor Red "Import failed`n";
    Write-Host -ForegroundColor Red "`nException Type: $($_.Exception.GetType().FullName)`n" $false;
    Write-Host -ForegroundColor Red "`nException Message: $($_.Exception.Message)`n" $false;
  }
}
else { Write-Host "Module already imported"; }

That looks better! Now, let’s move onto Part 3: Connecting and disconnecting to SPOnline.

References

  • Import-Module cmdlet – https://technet.microsoft.com/en-us/library/hh849725.aspx