How to create site collections and webs in SharePoint 2013 with PowerShell

PowerShell is one kind of script language which is based on .NetFramwork and is used for automatic management. For any operation completed within the UI in the SharePoint platform; you can also use SharePoint PowerShell to do the same thing. Not only can this improve work efficiency, but also helps you to further your knowledge about SharePoint. In this blog I will introduce how to create some SharePoint objects using PowerShell. Note that you will not only create these objects, but also use this as an opportunity to learn SharePoint cmdlet.

Generally, we know SharePoint nouns such as web application, site collection, sub site, list, etc. We can use these nouns as part of a command; just add “sp” and the noun, it should look like this: spwebapplication, spsite, etc. You can then further add some verbs to these noun parts of a command, such as new, set, get, remove, etc., to start forming commands.

Now let’s try creating one site collection.

Let’s assume you know the noun, but you do not known the correct command. You can get it with this command.

spsite -?

When you enter this command you will get all the commands that include spsite, like this:

create-site-collections-1

Let’s assume that you now know which command to use, but still do not know the specific parameters. You can use the

New-SPSite -?

command to get further information about the specific parameters, like this:

create-site-collections-2

If you want to see a more detailed example of the entire command, then use the

get-help new-spsite -examples

command, like this:

create-site-collections-3

We will use the second example,

New-SPSite http://farm05:9999/sites/testing

-OwnerAlias "farm05\spfarm05" -Name "Testing" -Template "STS#1"

After executing this command, one site collection will be created and the web template will be Team Site. You can get all the web templates with the Get-SPWebTemplate command.

I will not introduce other objects, but will assume that you understand the basic idea of how to create them, even if you are a newbie.

The last thing I will introduce is how to batch create test data, such as different lists in one team site.

You can do this with a loop. First you can get the count of different list templates and think about the value of the list name. Here we will use the template’s name as the list name; this way you will know every list’s template clearly after you create them. With the index, you can get a list template. Finally you will need to use the following statement:

$listcount=$web.ListTemplates.count

for($x=0;$X -lt $listcount;$x++)

{

$listname=$web.ListTemplates[$x].name

$web.lists.add("$listname","created by powershell",$web.listtemplates[$X]

}

By default, this will not display the list on quick launch.

But if you modify one property of all the lists in one loop, you will be able to complete it.

$lists=$web.Lists

$lists |foreach-object

{

$_.OnQuickLaunch="True"

$_.update()

}

After executing the above command, you can access this web to check the created lists, as shown in this screenshot:

create-site-collections-4

Now you too can create great things using SharePoint PowerShell.

2 Responses

  1. Megan Stayrook
    Megan Stayrook at |

    Hi can I say thats one of the better articles I have actually read in a very long time !

  2. Cheyenne Lacusky
    Cheyenne Lacusky at |

    Lovely article. if you want to learn more just visit – http://www.articlebuildingxxx777.com

Leave a Reply