Automating SharePoint Online Tasks (O365) using PowerShell + CSOM – Part 1

Today I am going to start a new series of automating some of the SharePoint online (O365) tasks using the PowerShell and Client Side Object Model (CSOM). Anyone who have worked on SharePoin Online knows that there are not as many PowerShell cmdlets as On-Prem. So if you used to automate tasks using PowerShell in On-Prem scenario, you will soon find that SharePoint Online does not have that flexibility. The option is to extend this using PowerShell and CSOM.
I am not going to be going in too much detail on this as Chris O’Brien have posted a really good blog post on this. You should read that article first before proceeding.
Basically what I am going to post is code on different SharePoint Online tasks. In next few parts what I will be covering is how you can create Content Types, Site Columns, Add Site Columns to Content Type and then Add Content Type to a List.
In this blog post I will show you, how you can access a web and then a list using PowerShell and CSOM. Once  you get the list how you will change Allow Management of Content Type option to True.
So lets start.
First step is to Authenticate against your O365 SharePoint site. If you have read the blog above by Chris, it has the code for the same. I would say create a PS1 file using that code and then use that code in your any other code. So for the sack of this blog post I am assuming that you copied the code from above blog post and called that PowerShell AuthenticateO365.ps1.
So next step is to Get List and then enable Allow Management of Content Type option. Here is the code for that.
# ……START Authenticate………………..
    .\1_Authenticate.ps1
    #……..END Authenticate…………………
    #……..START – Update List……………………
    $web = $ctx.Web 
    $list = $web.Lists.GetByTitle(“SPSBoston”)
    $ctx.Load($web) 
    $ctx.Load($list)
    $ctx.ExecuteQuery()
    Write-Host “Current list title is ‘$($list.Title)'” -ForegroundColor Yellow
    Write-Host “”
    $list.ContentTypesEnabled = $true
    $list.Update()
    $ctx.Load($list)
    $ctx.ExecuteQuery()
    Write-Host “List Content Type Enabled: ” $list.ContentTypesEnabled -ForegroundColor Yellow
    #……..END – Update List……………​