Running PowerShell with SharePoint on premises is pretty easy, you jump on the server and run up PowerShell or PowerShell ISE and start connecting and commanding. So how do you connect to SharePoint online from either a server or your local machine?
You need to install Windows Management Framework 3.0 and SharePoint Online Management Shell. The cool thing about SharePoint online and cmdlets is that you can run them from any machine with the tools installed.
Create a new PowerShell script by either opening a new notepad document and saving it as a '.ps1' file, or by opening PowerShell ISE. Paste in the following code and change the user, password and URLs.
Clear-Host $spOnlineUser = '' $spOnlinePassword = '' $spOnlineSiteAdminUrl = 'https://pkswlgsandbox-admin.sharepoint.com' $spOnlineSiteUrl = 'https://pkswlgsandbox.sharepoint.com/sites/devicechannels' $cred = New-Object -TypeName System.Management.Automation.PSCredential -argumentlist $spOnlineUser, $(convertto-securestring $spOnlinePassword -asplaintext -force) Connect-SPOService -Url $spOnlineSiteAdminUrl -Credential $cred Write-Host "Connecting to " $spOnlineSiteUrl $site = Get-SPOSite -Identity $spOnlineSiteUrl Write-Host "Success!!" -ForegroundColor "Green" $site
Now that you have a .ps1 file ready, run SharePoint Online Management Shell as Administrator and run your script.
Quick side note, if you get the following error "….ps1 cannot be loaded because running scripts is disabled on this system….", you need to run the following command "Set-ExecutionPolicy RemoteSigned" and click "Yes".
In SharePoint Online Management Shell:
In PowerShell ISE:
Once you have scripts running, and if you have set your credentials and URL's correctly, you should see the following:
I love PowerShell ISE, so if your planning on writing many scripts you should use it. It allows editing and running of scripts on one screen as well as intellisense and debugging.
So, what can you actually do against SharePoint online?? The cmdlets available out of the box are very limited, which means you need to write CSOM in your PowerShell. Here is a post by Chris O'Brien with more information on CSOM in PowerShell. You can also start to use custom CMDlets:
- https://sharepointonlineautomationcmdlets.codeplex.com/ – Download the custom CMDlets written by Provoke
- https://github.com/OfficeDev/PnP/tree/master/Solutions/PowerShell.Commands – Check out the custom CMDlets made available by the Office 365 Pattern and Practices site