Use the code below to create a new User Custom Action which will automatically run on ALL pages within a site collection, this is very useful for creating global design themes or code files that you want to run across all sites within a site collection (including the site collection top level site)

[csharp]</p> <p>$username = "someone@domain.com"<br> $password = Read-Host -Prompt "Please enter your password" -AsSecureString<br> $url = "https://tenant.sharepoint.com/sites/sitename"</p> <p>Add-Type -Path "c:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.dll"<br> Add-Type -Path "c:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"</p> <p># connect/authenticate to SharePoint Online and get ClientContext object..<br> $clientContext = New-Object Microsoft.SharePoint.Client.ClientContext($url)<br> $credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($username, $password)<br> $clientContext.Credentials = $credentials</p> <p>if (!$clientContext.ServerObjectIsNull.Value)<br> {<br> Write-Host "Connected to SharePoint Online site: ‘$Url’" -ForegroundColor Green<br> }</p> <p>$site = $clientContext.Site<br> $sca = $site.UserCustomActions;<br> $clientContext.Load($sca)<br> $clientContext.ExecuteQuery()</p> <p>$newCustomAction = $sca.Add()<br> $newCustomAction.Name = "DisplayNameOfAction" #give it a name – a date format usually works well<br> $newCustomAction.Sequence = "1000" #set the sequence of when the file should be loaded – the higher the number it is then it will be loaded after the other files<br> $newCustomAction.Title = "Global Brading JS and CSS"<br> $newCustomAction.Description = "Global Branding JS and CSS script link added by Username on 02/08/2017"<br> $newCustomAction.RegistrationType = "None"<br> $newCustomAction.Location = "ScriptLink" #leave this as is<br> $newCustomAction.ScriptSrc = "https://tenant.sharepoint.com/_catalogs/masterpage/JS/somefile.js" #URL to the file in question<br> $newCustomAction.Update() #apply the changes<br> $clientContext.Load($newCustomAction)<br> $clientContext.ExecuteQuery()</p> <p>[/csharp]