Working with a customer this week there was a desire to create a script to redirect users from one site to another (mostly because we had several sites to do this to). While there are many ways to accomplish this such as IIS or using the Content Editor Web Part with a traditional redirect script, SharePoint also provides a redirect page content type that provides this functionality. Below is an example script we created to achieve the creation of the old site URL, create a redirect page with the property that redirects to the new site and sets the redirect page as the home page of the site.
Add-PSSnapinMicrosoft.SharePoint.PowerShell
#Setup Site String Variables
$Site="http://intranet.contoso.com/sites/Proj/"
$SiteName="Project1"
$site1=$site+$SiteName
#Specify the site to redirect to
$Site1Redirect="http://intranet.contoso.com/PM/Projects/Project1"
#create the old site in the new environment to redirect from
new-spweb-url$site1-Template"CMSPublishing#0"-Name$SiteName
#Create Redirect Page
$s1=get-spweb$site1
#Finds the appropriate page layout
$pubWeb=[Microsoft.SharePoint.Publishing.PublishingWeb]::GetPublishingWeb($s1)
$pl=$pubWeb.GetAvailablePageLayouts() |Where { $_.Name -eq“redirectpagelayout.aspx”}
#Create New Page
$newPage=$pubWeb.AddPublishingPage("Redirect.aspx",$pl) #filename need end with .aspx extension
$newPage.Update()
#Get the docs from the Pages library and store in variable
$list=$s1.lists["Pages"]
$items=$list.items
#Go through all items and update the redirect page to point to the proper
foreach($itemin$items)
{
#If the "Name" column value equals "Redirect.aspx"
if($item["Name"]-eq"Redirect.aspx")
{
#Change the value of the "Redirect URL" column
$item["Redirect URL"]=$Site1Redirect
#Update the item
$item.Update()
}
}
#Check the file in and publish it
$newPage.CheckIn(“Created by PowerShell”);
$newPage.ListItem.File.Publish("")
#Configure Redirect Page as Home Page for each site
$HomeDL=$s1.RootFolder
$HomeDL.WelcomePage ="Pages/Redirect.aspx"
$HomeDL.update()