The first steps
The very first steps to connect to Sharepoint Online with Powershell are:
- load the required dlls for your operation ( you will need at a minimum Microsoft.SharePoint.CLient.dll and Microsoft.SharePointClient.Runtime.dll)
- establish a client context to the site where you want to work
Loading the dlls:
if you installed Sharepoint Online Management Shell :
$programFiles = [environment]::getfolderpath("programfiles")
add-type -Path $programFiles'\SharePoint Online Management Shell\Microsoft.Online.SharePoint.PowerShell\Microsoft.SharePoint.Client.dll'
add-type -Path $programFiles'\SharePoint Online Management Shell\Microsoft.Online.SharePoint.PowerShell\Microsoft.SharePoint.Client.Runtime.dll'
if you downloaded the NuGet package and extracted it to C:\Tools
add-type -path C:\Tools\microsoft.sharepointonline.csom.16.1.6906.1200\lib\net40-full\microsoft.sharepoint.client.dll
add-type -path C:\Tools\microsoft.sharepointonline.csom.16.1.6906.1200\lib\net40-full\microsoft.sharepoint.client.Runtime.dll
Establishing a context:
if ([string]::IsNullOrEmpty($url)){$url=Read-Host "Site Url?"}
$ctx = new-object Microsoft.SharePoint.Client.ClientContext($url)
if ([string]::IsNullOrEmpty($c)) {$c=Get-Credential -Message "Please enter the site collection Administrator credentials" }
$ctx.Credentials = new-object Microsoft.SharePoint.Client.SharePointOnlineCredentials($c.UserName,$c.Password) -Verbose
$ctx.ExecuteQuery()
All you need to do now is to access the site or web, the lists inside it, the list elements, etc.
example: