Office 365 PowerShell connection script

If you working with Office 365 PowerShell on daily basis, you may find that it is taking too much time to connect your Office 365 console, especially if you working with multiple tenants. You have to note usernames, passwords for all of the tenants and copy/paste them every time you want to make connection. Below you will find description, how to make it “single command” task.

Because that script will give access to your tenant without typing any username/password, I would suggest that you should not implement that on any public machine. Make sure that you are only person who can execute connection script.

OK, let’s start.

First of all we need to create addition file where encrypted tenant’s password will be stored. As an example create directory “O365Connections” on your C: drive and then create folder “keys” in it.

Start PowerShell prompt and type following command:

Read-Host -Prompt "Enter your tenant password" -AsSecureString | ConvertFrom-SecureString | Out-File "C:\O365Connections\keys\tenant1.key"

When you will be asked for password, type your tenant’s administrator password. That will create file with encrypted password in it.

After password file is created, create new. ps1 file in C:\O365Connections. That will be your connection script. I will use Connect-Tenant1.ps1 for name, you can use any name you think is good for you.

Copy and paste following code into newly created file:

Import-Module MSOnline

$TenantUname = "your_tenant_admin@yourtenant.onmicrosoft.com"
$TenantPass = cat "C:\O365Connections\keys\tenant1.key" | ConvertTo-SecureString
$TenantCredentials = new-object -typename System.Management.Automation.PSCredential -argumentlist $TenantUname, $TenantPass

$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell/ -Credential $TenantCredentials -Authentication Basic -AllowRedirection

Import-PSSession $Session -AllowClobber

Connect-MsolService -Credential $TenantCredentials

$OrgName = Get-MsolCompanyInformation | select -exp DisplayName
$InitialDomain = Get-MsolCompanyInformation | select -exp InitialDomain
$host.ui.RawUI.WindowTitle = "You are connected to: " + $OrgName + " (" + $InitialDomain + ") "

You need to replace “your_tenant_admin@yourtenant.onmicrosoft.com” with your tenant Admin user name. Save the file.

Before connecting to your tenant do not forget to install MSOnline PowerShell module and Microsoft Online Services Sign-In Assistant:

http://technet.microsoft.com/en-us/library/jj151815.aspx

If you running first script on the system, you probably need to change script execution policy:

Set-ExecutionPolicy -ExecutionPolicy Unrestricted

Please note that PowerShell prompt should be executed using “Run as Administrator” option in order to change script execution policy.

Now you can connect your tenant simply typing  following command:

C:\O365Connections\Connect-Tenant1.ps1

Your connection window should look like that:

O365_PS2

Please note that window title will show tenant’s domain and Organization name you are connected to.

You can create as multiple connection scripts using that approach, so it will make you everyday Office 365 administration more pleasant.

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.