TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
C# Corner
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Upload Master Page and Make It as a Default Master Page Using PowerShell in SharePoint
Sushant Kesari
Dec 10
2015
Code
4.3
k
0
0
facebook
twitter
linkedIn
Reddit
WhatsApp
Email
Bookmark
expand
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
Upload master page
make it as a default master page
PowerShell
SharePoint