- Write-Host “Upload master page to Master Page Gallaery and make this master page is Default master page to site Operation creating” -ForegroundColor Yellow
- Add-PSSnapin microsoft.sharepoint.powershell -ErrorAction SilentlyContinue
- $WebURL = Read-Host "Please enter your SiteCollection Url"
- $MasterPageName = "CRSMasterPage.master"
- $SourcePath ="C:\scripts\MyMasterPage.master"
- #Get the Web
- $web = Get-SPWeb $WebURL
- #Get the Target folder - Master page Gallery
- $MasterPageList = $web.GetFolder("Master Page Gallery")
- #Set the Target file for Master page
- $TargetPath = $Web.Url + "/_catalogs/masterpage/" + $MasterPageName
- #Get the Master page from local disk
- $MasterPageFile = (Get-ChildItem $SourcePath).OpenRead()
- #Check if file exist already
- if ($Web.GetFile($TargetPath).Exists)
- {
- $Web.GetFile($TargetPath).recycle()
- }
- #upload master page using powershell
- $MasterPage = $MasterPageList.Files.Add($TargetPath,$MasterPageFile,$false)
- #Prepare the Custom Master page URL
- $WebURL = $web.ServerRelativeUrl.TrimEnd("/")
- $MasterPageURL = $WebURL+"/_catalogs/masterpage/" + $MasterPageName
- #Set Default and Custom Master pages
- $web.MasterUrl = $MasterPageURL
- $web.CustomMasterUrl = $MasterPageURL
- #Apply Changes
- $MasterPage.Update()
- $MasterPage.Publish("")
- $web.Update()
- $web.Dispose()
- Write-Host “Add Default Master Page to Site Operation Completed Successfully” -ForegroundColor Yellow
This code snippet for Upload master page to Master Page Gallaery and make this master page is Default master page using powerShell in Sharepoint
