Quick connect to O365 using PowerShell

I’ve been doing lots of posts about connecting to Office365, and a bunch of scripts, so I thought I’d share my easy script – just gotta update the URL, and PASSWORD.

** NB.  You need a folder with the DLL’s – this is my set of files :

image

This is the basic script – to add a reference to the necessary DLL’s (above) – and then define the user/password – and make a connection – and then load the WEB object.

$path =  'C:\o365';

$urlSite = 'https://[tenant].sharepoint.com/sites/[SITECOLLECTION]'

$user = '[USER]@[TENANT].onmicrosoft.com';
$password = 'Password1234';

[Reflection.Assembly]::LoadFile('$path\Libraries\Microsoft.SharePoint.Client.dll')
[Reflection.Assembly]::LoadFile('$path\Libraries\Microsoft.SharePoint.Client.Runtime.dll')

add-type -Path $path'\Libraries\Microsoft.SharePoint.Client.dll'
add-type -Path $path'\Libraries\Microsoft.SharePoint.Client.Runtime.dll'

$passwordSecureString = ConvertTo-SecureString -string $password -AsPlainText -Force
$credential = New-Object -TypeName System.Management.Automation.PSCredential -argumentlist $user, $passwordSecureString

$spoCtx = New-Object Microsoft.SharePoint.Client.ClientContext($urlSite)
$spoCredentials = New-Object -TypeName Microsoft.SharePoint.Client.SharePointOnlineCredentials -argumentlist $user, $passwordSecureString
$spoCtx.Credentials = $spoCredentials
$spoCtx.RequestTimeout = '500000'

$web = $spoCtx.web
$spoCtx.Load($web)
$spoCtx.ExecuteQuery();

Please let me know if that’s helpful – feel free to copy+use – I’ve used this a LOT in quick testing scripts…

🙂

Advertisements

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s