PowerShell Create Site Collection

This is the PowerShell I use to create site collections. I have variables to determine which Web App I’m installing to (there are two on my farm), and which admin is running the installation. This script also turns on several site collection features, adds URLS to the top navigation bar, creates custom permission group, removes permission groups created by the publishing feature that I don’t want to use, and then adds the newly created site collection to a site navigation listing on the root web app.

Cls
$snapIn = Get-PSSnapin | where-object {$_.Name -eq "Microsoft.SharePoint.PowerShell"}
 if($snapIn -eq $null) {
 Add-PsSnapin Microsoft.SharePoint.PowerShell
 }
# REMEMBER TO INCREASE THE CONTENT DB AUTOGROWTH !!!
# Set WebApp to WEBAPP1 or WEBAPP2 ****
 #$webAPP = "WEBAPP1"
 $webAPP = "WEBAPP2"
# Set user running Script ****
 $scriptAdmin = "ADMIN1"
 #$scriptAdmin = "ADMIN2"
$WebsiteName = "SITENAME"
 $WebsiteDesc = "SITEDESCRIPTION"
 # Override Acronym Value if you want to specify, otherwise set to "XXX" and let it be calculated ****
 $acronym = "ACRO"
# STS#0 Team Site | STS#1 Blank Site | STS#2 Document Workspace | WIKI#0 Wiki Site | BLOG#0 Blog | SGS#0 Group Work Site
 $Template = "STS#1"
if ($webAPP.Contains('INT')){
 $server = "https://WebApp2.COM"
 $homeroot = "DOMAIN Internal"
 }
 else {
 $server = "https://WebApp1.COM"
 $homeroot = "DOMAIN Public"
 }
if ($scriptAdmin.Contains('ADMIN1')){
 $SecondaryLogin = "DOMAIN\ADMIN1"
 $adfsLogin = "i:0#.w|DOMAIN\ADMIN1"
 $SecondaryLoginDisplay = "ADMIN1"
 $SecondaryEmail = "ADMIN1@EMAIL.COM"
 }
 else {
 $SecondaryLogin = "DOMAIN\ADMIN2"
 $adfsLogin = "i:0#.w|DOMAIN\ADMIN2"
 $SecondaryLoginDisplay = "ADMIN2"
 $SecondaryEmail = "ADMIN2@EMAIL.COM"
 }
 $databaseServer = "DATABASESERVER"
 $ContentDatabase = $webAPP + "_Content_" + $WebsiteName.replace(" ","")
 $PrimaryLogin = "DOMAIN\SPADMINSERVICE"
 $PrimaryEmail = "SPADMINSERVICE@EMAIL.COM"
 $allAdmins = ("i:0#.w|DOMAIN\ADMIN1", "i:0#.w|DOMAIN\ADMIN2")
if ($acronym.Contains('XXX')){
 if ($WebsiteName.Contains(' ')) {
 $workingName = $WebsiteName
 $acronym = ""
 while ($workingName.Contains(' ')){
 $acronym = $acronym + $workingName.ToUpper().Substring(0,1)
 $workingName = $workingName.Substring($workingName.IndexOf(' ')+1)
 }
 $acronym = $acronym + $workingName.ToUpper().Substring(0,1)
 }
 else {
 $acronym = $WebsiteName.ToUpper().Substring(0,3)
 }
 }
$siteURL = $server + "/sites/" + $acronym
write-host "Url $siteURL ContentDatabase $ContentDatabase Name $WebsiteName Description $WebsiteDesc Template $Template OwnerAlias $PrimaryLogin OwnerEmail $PrimaryEmail SecondaryOwnerAlias $SecondaryLogin SecondaryEmail $SecondaryEmail"
New-SPContentDatabase $ContentDatabase -DatabaseServer $databaseServer -WebApplication $server
New-SPSite -Url $siteURL –ContentDatabase $ContentDatabase -Name $WebsiteName –Description $WebsiteDesc -Template $Template -OwnerAlias $PrimaryLogin –OwnerEmail $PrimaryEmail -SecondaryOwnerAlias $SecondaryLogin -SecondaryEmail $SecondaryEmail
Enable-SPFeature PublishingPrerequisites -URL $siteURL
 Enable-SPFeature PublishingResources -URL $siteURL
 # SharePoint Server Publishing Infrastructure
 Enable-SPFeature -id f6924d36-2fa8-4f0b-b16d-06b7250180fa -URL $siteURL
 # SharePoint Server Publishing
 Enable-SPFeature -id 94c94ca6-b32f-4da9-a9e3-1f3d343d7ecb -URL $siteURL
 # SharePoint Server Standard Site Collection features
 Enable-SPFeature -id b21b090c-c796-4b0f-ac0f-7ef1659c20ae -URL $siteURL
 # Register taxonomy site wide field added event receiver (Content Type Publishing)
 Enable-SPFeature -id 73EF14B1-13A9-416b-A9B5-ECECA2B0604C -URL $siteURL
# ADD ROOT SITE TO NAVIGATION
$SPSite = new-object Microsoft.SharePoint.SPSite($siteurl);
 $OpenWeb = $SPSite.OpenWeb();
function Add-SPSiteToTopNav([string]$url,[string]$Site,[string]$SiteURL) {
$TopNavBar = $OpenWeb.Navigation.TopNavigationBar
$Node = New-Object Microsoft.SharePoint.Navigation.SPNavigationNode $Site, $SiteUrl, 1
 $TopNavBar.AddAsLast($Node)
 }
Add-SPSiteToTopNav -url $siteURL -Site $homeroot -SiteURL "/SitePages/Home.aspx"
 Add-SPSiteToTopNav -url $siteURL -Site "SharePoint Training" -SiteURL "https://WebApp1.COM/sites/TRN"
# CREATE SITE PERMISSIONS
function fnNewGroup {
 $OpenWeb.SiteGroups.Add($groupName,$groupOwner,$OpenWeb.CurrentUser,$groupDesc);
 $OpenWeb.AssociatedGroups.Add($OpenWeb.SiteGroups[$groupName]);
$roleAssignment = new-object Microsoft.SharePoint.SPRoleAssignment($OpenWeb.SiteGroups[$GroupName]);
 $roleAssignment.RoleDefinitionBindings.Add($OpenWeb.RoleDefinitions[$groupRoll]);
$OpenWeb.RoleAssignments.Add($roleAssignment);
 }
function fnNewPermSet {
 $roleDefinition = new-object Microsoft.SharePoint.SPRoleDefinition;
 $roleDefinition.Name = $roleName;
 $roleDefinition.Description = $roleDesc;
 $roleDefinition.BasePermissions = $rolePerms;
$OpenWeb.RoleDefinitions.Add($roleDefinition);
 }
$groupName = "$acronym Full Control";
 $groupRoll = "Full Control";
 $groupDesc = "Use this group to give people $groupRoll permissions to the SharePoint site: $WebsiteName. Must have experience in SharePoint administration and be a staff member of the IT department.";
 $groupOwner = $OpenWeb.SiteAdministrators[0];
fnNewGroup;
# Add site collection administrators
foreach($siteAdmin in $allAdmins)
 {
 # Add user to Site Colleciton Administrator
 New-SPUser -UserAlias $siteAdmin -web $siteURL -SiteCollectionAdmin
# Add user to Full Control Group
 $user = Get-SPUser -Identity (New-SPClaimsPrincipal -Identity $siteAdmin -IdentityType WindowsSamAccountName).ToEncodedString() -Web $siteURL
 Set-SPUser -Identity $user -Web $siteURL -Group $groupName
 }
$roleName = "Site Manager";
 $roleDesc = "Limited Full Control - Cannot Create Subsites, Apply Themes and Borders, nor Create Groups.";
 $rolePerms = "ViewListItems,AddListItems,EditListItems,DeleteListItems,ApproveItems,OpenItems,ViewVersions,DeleteVersions,CancelCheckout,ManagePersonalViews,ManageLists,ViewFormPages,Open,ViewPages,AddAndCustomizePages,ViewUsageData,BrowseDirectories,BrowseUserInfo,AddDelPrivateWebParts,UpdatePersonalWebParts,UseClientIntegration,UseRemoteAPIs,ManageAlerts,CreateAlerts,EditMyUserInfo,EnumeratePermissions";
 $groupName = "$acronym $roleName";
 $groupRoll = $roleName;
 $groupDesc = "Use this group to give people $groupRoll permissions to the SharePoint site: $WebsiteName. Must have SharePoint experience or training and be a member of the agency designated as the owner of the subsite where the user will have access.";
 $groupOwner = $OpenWeb.SiteGroups["$acronym Full Control"];
fnNewPermSet;
 fnNewGroup;
$roleName = "Power User";
 $roleDesc = "Limited Full Control - Cannot Apply Themes and Borders.";
 $rolePerms = "ManageLists, CancelCheckout, AddListItems, EditListItems, DeleteListItems, ViewListItems, ApproveItems, OpenItems, ViewVersions, DeleteVersions, CreateAlerts, ViewFormPages, ManagePermissions, ViewUsageData, ManageSubwebs, ManageWeb, AddAndCustomizePages, CreateGroups, BrowseDirectories, ViewPages, EnumeratePermissions, BrowseUserInfo, ManageAlerts, UseRemoteAPIs, UseClientIntegration, Open, EditMyUserInfo, ManagePersonalViews, AddDelPrivateWebParts, UpdatePersonalWebParts";
fnNewPermSet;
$roleName = "User Admin";
 $roleDesc = "Limited Site Manager - Cannot Manage Lists, Override Check Out, Add or Customize Pages, or Manage Alerts.";
 $rolePerms = "ViewListItems,AddListItems,EditListItems,DeleteListItems,ApproveItems,OpenItems,ViewVersions,DeleteVersions,ManagePersonalViews,ViewFormPages,Open,ViewPages,ViewUsageData,BrowseDirectories,BrowseUserInfo,AddDelPrivateWebParts,UpdatePersonalWebParts,UseClientIntegration,UseRemoteAPIs,CreateAlerts,EditMyUserInfo,EnumeratePermissions";
 $groupName = "$acronym $roleName";
 $groupRoll = $roleName;
 $groupDesc = "Use this group to give people $groupRoll permissions to the SharePoint site: $WebsiteName. The user admin is an expert in the content showcased on the site or page and likely will need to learn about SharePoint Server; but their primary expertise is business focused.";
 $groupOwner = $OpenWeb.SiteGroups["$acronym Site Manager"];
fnNewPermSet;
 fnNewGroup;
$roleName = "Access Web Contributor";
 $roleDesc = "Contributor for Access Services Web Database without ability to open database in Access 2010.";
 $rolePerms = "ViewListItems,AddListItems,EditListItems,DeleteListItems,Open,ViewPages,BrowseUserInfo";
 $groupName = "$acronym $roleName";
 $groupRoll = $roleName;
 $groupDesc = "Use this group to give people $groupRoll permissions to the SharePoint site: $WebsiteName.";
 $groupOwner = $OpenWeb.SiteGroups["$acronym Site Manager"];
fnNewPermSet;
 fnNewGroup;
$roleName = "Contributors";
 $groupName = "$acronym $roleName";
 $groupRoll = "Contribute";
 $groupDesc = "Use this group to give people $groupRoll permissions to the SharePoint site: $WebsiteName.";
 $groupOwner = $OpenWeb.SiteGroups["$acronym Site Manager"];
fnNewGroup;
$roleName = "Visitors";
 $groupName = "$acronym $roleName";
 $groupRoll = "Read";
 $groupDesc = "Use this group to give people $groupRoll permissions to the SharePoint site: $WebsiteName.";
 $groupOwner = $OpenWeb.SiteGroups["$acronym Site Manager"];
fnNewGroup;
$roleName = "Edit-Add";
 $roleDesc = "Can view, add, and update. Cannot delete.";
 $rolePerms = "ViewListItems,AddListItems,EditListItems,OpenItems,ViewVersions,ViewFormPages,Open,ViewPages,BrowseDirectories,BrowseUserInfo,UseRemoteAPIs,UseClientIntegration,CreateAlerts";
 $groupName = "$acronym $roleName";
 $groupRoll = $roleName;
 $groupDesc = "Use this group to give people $groupRoll permissions to the SharePoint site: $WebsiteName. Can view, add, and update. Cannot delete.";
 $groupOwner = $OpenWeb.SiteGroups["$acronym Site Manager"];
fnNewPermSet;
 fnNewGroup;
$roleName = "Add Only";
 $roleDesc = "Can view and add. Cannot update or delete.";
 $rolePerms = "ViewListItems,AddListItems,OpenItems,ViewVersions,ViewFormPages,Open,ViewPages,BrowseDirectories,BrowseUserInfo,UseRemoteAPIs,UseClientIntegration,CreateAlerts";
 $groupName = "$acronym $roleName";
 $groupRoll = $roleName;
 $groupDesc = "Use this group to give people $groupRoll permissions to the SharePoint site: $WebsiteName. Can view and add. Cannot update or delete.";
 $groupOwner = $OpenWeb.SiteGroups["$acronym Site Manager"];
fnNewPermSet;
 fnNewGroup;
$OpenWeb.SiteGroups.Remove("Approvers");
 $OpenWeb.SiteGroups.Remove("Designers");
 $OpenWeb.SiteGroups.Remove("Hierarchy Managers");
 $OpenWeb.SiteGroups.Remove("Viewers");
 $OpenWeb.SiteGroups.Remove("Quick Deploy Users");
 $OpenWeb.SiteGroups.Remove("Restricted Readers");
# Add entry to Site Directory List
$spScope = Start-SPAssignment
 $mylist = ($siteScope | Get-SPWeb -identity $server).GetList("Lists/SiteDirectory")
 $newItem = $mylist.AddItem()
 $newItem["Title"] = $WebsiteName
 $newItem["Description"] = $WebsiteDesc
 $newItem["URL"] = "$siteURL, $WebsiteName"
 $newItem["Top Site"] = "Yes"
 $newItem.Update()
 Stop-SPAssignment $spScope
$OpenWeb.Update();
 $OpenWeb.Dispose();
 $SPSite.Dispose();
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: