PowerShell to Inject JavaScript or CSS using Custom Action in SharePoint

Requirement: Inject JavaScript or CSS in SharePoint without editing Master Page.

PowerShell to Inject JavaScript in SharePoint using Custom Action:

Assuming the “ga.js” file is already uploaded to the Site Assets library, here is the PowerShell for JavaScript injection in the SharePoint master page.

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

#Set Site variable
$SiteURL="https://intranet.crescent.com"
$CustomActionTitle ="Google Analytics"

Try {
    #Get the Web
    $Web = Get-SPWeb $SiteURL
 
    #Check if the Custom Action Exists already
    $CustomAction = $web.UserCustomActions | Where { $_.Title -eq $CustomActionTitle }

    If($CustomAction -eq $Null)
    {
        #Add new custom action
        $UserCustomAction = $web.UserCustomActions.Add()

        #Set the Properties of the custom action
        $UserCustomAction.Title = $CustomActionTitle
        $UserCustomAction.Location = "ScriptLink"
        $UserCustomAction.ScriptSrc = "~sitecollection/SiteAssets/ga.js"
        $UserCustomAction.Sequence = 1000
        $UserCustomAction.Update()
 
        Write-Host -f Green "Custom Action Added Successfully!"
    }
    Else
    {
        write-host -f Yellow "Custom Action Already Exists!"
    } 
} Catch {
    Write-Host -ForegroundColor Red "Error:" $_.Exception.Message
}

Insert CSS using Custom Action – PowerShell

Similarly, you can insert CSS to the master page using PowerShell.

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

#Set Site variable
$SiteURL="https://intranet.crescent.com"
$CustomActionTitle ="Custom Branding CSS"

Try {
    #Get the Site
    $Site = Get-SPSite $SiteURL
 
    #Check if the Custom Action Exists already
    $CustomAction = $Site.UserCustomActions | Where { $_.Title -eq $CustomActionTitle } | Select -First 1

    #Update the Custom action if found already
    If($CustomAction -eq $Null)
    {
        #Add new custom action
        $UserCustomAction = $Site.UserCustomActions.Add()
    }
    $CSSURL=$Site.URL+"/SiteAssets/Custom.css"
    #Set the Properties of the custom action
    $UserCustomAction.Title = $CustomActionTitle
    $UserCustomAction.Location = "ScriptLink"
    $UserCustomAction.ScriptBlock = "document.write('<link rel=""stylesheet"" After=""Corev15.css"" href=""$($CSSURL)"" type=""text/css""/>')"
    $UserCustomAction.Sequence = 1000
    $UserCustomAction.Update()
 
    Write-Host -f Green "Custom Action Updated Successfully!"
} Catch {
    Write-Host -ForegroundColor Red "Error:" $_.Exception.Message
}

Here is another post on JavaScript injection to SharePoint Online Master page Inject JavaScript into SharePoint Online

Salaudeen Rajack

Salaudeen Rajack - Information Technology Expert with Two-decades of hands-on experience, specializing in SharePoint, PowerShell, Microsoft 365, and related products. He has held various positions including SharePoint Architect, Administrator, Developer and consultant, has helped many organizations to implement and optimize SharePoint solutions. Known for his deep technical expertise, He's passionate about sharing the knowledge and insights to help others, through the real-world articles!

3 thoughts on “PowerShell to Inject JavaScript or CSS using Custom Action in SharePoint

  • Tried using your second snippet to inject stylesheet reference to a site through PS, and it worked BEAUTIFULLY. NOW, however, I need to modify it to do the exact same function on ALL of the sites in the farm. I am using a shared stylesheet that styles the ‘maintenance mode’ banner while we are migrating from SP Server 2016 to SP Online. I don’t know if that means lines 7-31 should be inside of a for loop or something or what? I don’t work inside of powershell much and am typically a front end person, but needed to convey this request to our SP Admin (who’s PS skills are somehow comparable to mine…) Would appreciate ANY guidance or direction about how to essentially convert
    $SiteURL=”{SPECIFIC-SITE-URL}”
    ** to correctly say this: **
    $SiteURL= { *DO THIS FUNCTION ONCE FOR EACH SITE RETURNED IN SITE COLLECTION *}
    Hope that makes sense. Seems like it should be an easy task!?!

    Reply
    • Try this:

      #Set Site variable
      $SitesColl = Get-SPSite -limit All
      $CustomActionTitle =”Custom Branding CSS”

      Foreach ($SiteURL in $SitesColl)
      {
      Try {
      #Get the Site
      $Site = Get-SPSite $SiteURL

      #Rest of script.

      Reply
  • When I do the JS injection, SharePoint 2010 site gives the error when loading
    An unexpected error occurred.
    When I remove the custom action site loads normally.
    Any suggestions for this issue?

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *