Background

Many of my days are spent finding solutions for Office 365 problems using PowerShell. After importing myriad Office 365 modules during any given day, I finally came to the conclusion that the time had come to create a start-up script that would load Office 365 modules whenever PowerShell opens.

Solution

I was pleased to discover that implementing my plan was pretty straightforward. I copied the common commands to import Office 365 modules and combined them into a single PowerShell script. After testing the script, I forced it to execute during the start-up of a PowerShell session.

Perform the following steps before you execute the script.

Note: Load the PowerShell Script in PowerShell ISE to match line numbers

The Integration of the Connect to Office 365 Services PowerShell script is fairly straightforward. In order to implement the Connect to Office 365 Services script on your workstation, follow the steps below.

  1. Copy all content in the script section and save it as a .PS1 file under C:\Temp\Connect\
    Connect-Office 365-Services.ps1
  2. Validate if you already have a profile. In PowerShell, type Test-Path $Profile
  3. If you don’t have a profile (result is $False), type New-Item -path $profile -type file –force
  4. In PowerShell, type If ((Test-Path $Profile) -eq “True”) {Notepad $Profile}
  5. Else {Write-Host “Still no profile file”}
  6. In the profile text file type:

    cd C:\temp\connect

    & ‘.\Connect-Office 365-Services.ps1’


  7. Close PowerShell
  8. The first time you open PowerShell, you will be asked to enter the administrator password. Upon subsequent openings, PowerShell will read your password from the secure .txt file, located under C:\Temp\Connect\Cred.


  9. It could take up to 15 seconds for PowerShell to load all of the Office 365 modules. To speed up the process, comment out certain modules in the script. For example, I comment out the Skype for Business module.
  10. Once modules load into the run space, most of the Office 365 commands are available in your PowerShell session.


Reference: http://www.computerperformance.co.uk/powershell/powershell_profile_ps1.htm

Log File

The log file tracks if a module is successfully loaded or not. The log file can be found under C:\Temp\Connect\Log\Log.txt.

Script

$logfile = ("C:\temp\Connect\Log\Subsite.log")
$PasswordFile = "C:\temp\Connect\cred.txt"
$AdminURI = "https://domain-admin.sharepoint.com"
$AdminAccount = "admin@domian.onmicrosoft.com"


function log{
param (
    [String]$text,
    [Switch]$fout
    )

ac $logfile $text
if($showConsoleOutput){
    if($fout){
        Write-Host $text -ForegroundColor Red
            }else{

Write-Host $text -ForegroundColor Green
        }

    }

}

log -text "-----$(Get-Date) Services - $($env:USERNAME) on $($env:COMPUTERNAME) starting-----"
 
$PasswordFileCheck = Test-Path $PasswordFile
If($PasswordFileCheck -eq $False){
    Read-Host -Prompt "Enter Admin Password" -AsSecureString | ConvertFrom-SecureString | Out-File "C:\temp\Connect\cred.txt"
    log -text "Created a password file under C:\temp\Connect"
    }
    Else{
        log -text "Password file already created under C:\temp\Connect"
        }

# Connect to Office 365 services
$Pass = Get-Content $PasswordFile | ConvertTo-SecureString
$Cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $AdminAccount, $Pass

Import-Module MSOnline
$Connect = Connect-MsolService -Credential $cred -ErrorAction SilentlyContinue -ErrorVariable ProcessError
If ($ProcessError) {
    log -text "------ Didn't Connect to Office 365 services"
    }
         Else{
log -text "Connected to Office 365 Services"
             }

Import-Module SkypeOnlineConnector
$Connect1 = New-CsOnlineSession -Credential $cred -ErrorAction SilentlyContinue -ErrorVariable ProcessError
Import-PSSession $Connect1
If ($ProcessError) {
log -text "------ Didn't Connect to Skype for Business services"
}
    Else{
    log -text "Connected to Skype for Business services"
        }

$Pass = Get-Content $PasswordFile | ConvertTo-SecureString
$Cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $AdminAccount, $Pass

$Connect2 = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri "https://outlook.office365.com/powershell-liveid/" -Credential $Cred -Authentication "Basic" -AllowRedirection -ErrorAction SilentlyContinue -ErrorVariable ProcessError
Import-PSSession $Connect2 -DisableNameChecking
If ($ProcessError) {
    log -text "------ Didn't Connect to Exchange Online"
    }
        Else{
        log -text "Connected to Exchange Online"
            }

$Pass = Get-Content $PasswordFile | ConvertTo-SecureString
$Cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $AdminAccount, $Pass

# Connect to SharePoint Online
$cred = Connect-SPOService -Url $AdminURI -Credential $cred -ErrorAction SilentlyContinue -ErrorVariable ProcessError
If ($ProcessError) {
    log -text "------ Didn't Connect to SharePoint Online services"
    }
        Else{
        log -text "Connected to SharePoint Online Services"
}