SharePoint Management Shell Scripts for Beginners

SharePoint Management Shell Scripts for Beginners

I had certain requirements to Create multiple Top  Level Sites  and felt that, for starters looping through a one line code would do the work. I would be adding in more Scripts here for other requirements too..!!

1.Given below is the Powershell Script for creating multiple Top-level Sites

Add-PSSnapin "Microsoft.SharePoint.PowerShell"

$url=$args[0]
$name=$args[1]
$ownerAlias=$args[2]
$ownerEmail=$args[3]
$template=$args[4]
$language=$args[5]
$siteCount=$args[6]

## SharePoint DLL
[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")


For($i=1; $i -le $siteCount; $i++)
{
Write-Output "Creating Top Level Site...!!!"
New-SPSite -Url ($url+$i) -Name ($name+"-"+$i) -OwnerAlias $ownerAlias -OwnerEmail $ownerEmail -Template $template -Language $language -Verbose
Write-Output "Top Level Site Created...!!!"
}


Remove-PSSnapin "Microsoft.SharePoint.PowerShell"

Arguments Description

$url --> Url of the New Top level Site to be created
$name --> Name or Title of the Site
$ownerAlias --> "Domain\user"
$ownerEmail --> email ID associated with the User
$template --> Template to be used in the Site Creation(Get-SPWebTemplate Commands lists out the different templates that could be used)
$language--> It is the Locale ID which is the Language used this Locale ID can be got from the link( https://msdn.microsoft.com/en-us/library/ms912047(v=winembedded.10).aspx ), but for rendering the Site in the desired language the appropriate Language Packs and their corresponding updates provided by Microsoft must be installed on the Server
$siteCount --> The number of Top Level Sites to be created.

Steps

1. Copy the Script in any Text Editor and save it as a .ps1 file(ex: CreateTopLevelSite.ps1) in let us say a folder in C:\Scripts

2. Open SharePoint Management Shell by going to Start--> SharePoint Management Shell. This would open the Management Shell as PS C:\somepath\someuser>.

3.Navigate to the folder location where the script has been saved using the command cd C:\Scripts or cd C:\Folder_name. You could also choose to create the script at a different folder location, it is not mandatory to create it in C:\, but just ensure that you execute the script from the right folder location

4. Pass the arguments $url=$args[0], $name=$args[1], $ownerAlias=$args[2],
$ownerEmail=$args[3],$template=$args[4],$language=$args[5],$siteCount=$args[6].
NB:You could vary the order of the arguments based on which you would have to supply the values

5. The Sites get created.

Here the main command for execution is the New-SPSite where the Required Parameters are URL, the OwnerAlias and the siteCount. The other Parameters are optional

Reference: https://technet.microsoft.com/en-us/library/ff607937.aspx

2.Given below is the Powershell Script for creating multiple Webs

Add-PSSnapin "Microsoft.SharePoint.PowerShell"

$web = $args[0]
$template = $args[1]
$webName = $args[2]
$webCount = $args[3]

## SharePoint DLL
[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")

  $startTime = (Get-Date)
   
  For($i=1; $i -le $webCount; $i++ )
{
Write-Output "Creating Web..!!"
Write-Output "Please Wait...!!"
$startTime1 = (Get-Date)

New-SPWeb -Url ($web+$i) -Template $template -Name ($webName+"-"+$i) -AddToTopNav -UseParentTopNav -Verbose
$endTime1 = (Get-Date)
   
Write-Output "Time taken for the web creation $($endTime1-$startTime1)"
Write-Output "Finished creation of Web!!"

}

  $endTime = (Get-Date)
   
  Write-Output "Total time taken for the job $($endTime-$startTime)"

Remove-PSSnapin "Microsoft.SharePoint.PowerShell"

Arguments Description

$web --> Url of the Web to be created
$template --> Template to be used in the Site Creation(Get-SPWebTemplate Commands lists out the different templates that could be used)
$webName --> Name or Title of the Web
$webCount --> The number of Top Level Sites to be created.
$startTime and $endTime--> Get the Start time and the End Time for the process using Get-Date command
$startTime1 and $endTime1 --> Start time and the End Time for creating each web

-AddToTopNav -->Adds this site to the top-level navigation bar.
-UseParentTopNav --> Specifies that the same top-level navigation bar as the parent site is to be used for this site.

Steps

1. Copy the Script in any Text Editor and save it as a .ps1 file(ex: CreateWebs.ps1) in let us say a folder in C:\Scripts

2. Open SharePoint Management Shell by going to Start--> SharePoint Management Shell. This would open the Management Shell as PS C:\somepath\someuser>.

3.Navigate to the folder location where the script has been saved using the command cd C:\Scripts or cd C:\Folder_name. You could also choose to create the script at a different folder location, it is not mandatory to create it in C:\, but just ensure that you execute the script from the right folder location

4. Pass the arguments $web=$args[0], $template=$args[1], $webName=$args[2],  $webCount=$args[3].
NB:You could vary the order of the arguments based on which you would have to supply the values

5. The Webs get created.

6. The -AddToTopNav and -UseParentTopNav are Optional too i.e., if you want to see the Webs on the Top Level Site

Here the main command for execution is the New-SPWeb where the Required Parameters is the web URL and the webCount. The other Parameters are optional

Reference: https://technet.microsoft.com/en-us/library/ff607579.aspx

NB: I have not taken any advanced level of Trust Settings into consideration here(i.e any system Level User Permissions or rights).







Comments