Microsoft Sharepoint 2010 is one of the new products which supports Windows Powershell commands. Its really easy to do the most tasks you do normally in the Sharepoint Central Administration with PowerShell.
Microsoft created a PowerShell Snapin for Sharepoint 2010, which is called “Microsoft.Sharepoint.Powershell”. This enables a lot of new PowerShell cmdlets for Sharepoint.
With this small scripts you can simply create a new WebApplication and/or a new Sharepoint Site Collection.
Add the Sharepoint cmdlets:
Add-PsSnapin Microsoft.SharePoint.PowerShell
Help for creating a new Sharepoint WebApplication:
The Powershell command for creating a new Web Application is New-SPWebApplication.
Get-Help New-SPWebApplication NAME New-SPWebApplication SYNOPSIS Creates a new Web application within the local farm. SYNTAX New-SPWebApplication -ApplicationPool -Name [-AdditionalClaimProvider <SPClaimProviderPipeBind[]> ] [-AllowAnonymousAccess ] [-ApplicationPoolAccount ] [-AssignmentCollec tion ] [-AuthenticationMethod ] [-AuthenticationProvider ] [-Confirm []] [-DatabaseCredentials ] [-DatabaseName ] [-Databas eServer ] [-HostHeader ] [-Path ] [-Port ] [-SecureSocketsLayer ] [-ServiceApplicationProxyGroup ] [-SignInRedirectProvider ] [-SignInRedirectURL ] [-Url ] [-WhatIf []] [] DESCRIPTION Creates a new Web application specified by the Name parameter. The user specified by the DatabaseCredentials parame ter must be a member of the dbcreator fixed server role on the database server. For permissions and the most current information about Windows PowerShell for SharePoint Products, see the online d ocumentation (http://go.microsoft.com/fwlink/?LinkId=163185). RELATED LINKS Get-SPWebApplication Set-SPWebApplication Get-SPWebApplication REMARKS To see the examples, type: "get-help New-SPWebApplication -examples". For more information, type: "get-help New-SPWebApplication -detailed". For technical information, type: "get-help New-SPWebApplication -full".
Creating a new Sharepoint WebApplication:
# SharePoint cmdlets Add-PsSnapin Microsoft.SharePoint.PowerShell # Set variables $WebAppName = "Contoso Sharepoint1" $WebAppHostHeader = "sharepoint1.contoso.com" $WebAppPort = 80 $WebAppAppPool = "SharePoint1AppPool" # This User has to be a Sharepoint Manager Account $WebAppAppPoolAccount = "contoso\sp_serviceuser" $WebAppDatabaseName = "Sharepoint1" $WebAppDatabaseServer = "SQLServer\InstanceName" # Create a new Sharepoint WebApplication New-SPWebApplication -Name $WebAppName -Port $WebAppPort -HostHeader $WebAppHostHeader -URL ("http://" + $WWebAppHostHeader) -ApplicationPool $WebAppAppPool -ApplicationPoolAccount (Get-SPManagedAccount $WebAppAppPoolAccount) -DatabaseName $WebAppDatabaseName -DatabaseServer $WebAppDatabaseServer
Creating a new Sharepoint Site Collection:
# SharePoint cmdlets Add-PsSnapin Microsoft.SharePoint.PowerShell # Templates # Name Title LocaleId Custom # ---- ----- -------- ------ # GLOBAL#0 Global template 1033 False # STS#0 Team Site 1033 False # STS#1 Blank Site 1033 False # STS#2 Document Workspace 1033 False # MPS#0 Basic Meeting Workspace 1033 False # MPS#1 Blank Meeting Workspace 1033 False # MPS#2 Decision Meeting Workspace 1033 False # MPS#3 Social Meeting Workspace 1033 False # MPS#4 Multipage Meeting Workspace 1033 False # CENTRALADMIN#0 Central Admin Site 1033 False # WIKI#0 Wiki Site 1033 False # BLOG#0 Blog 1033 False # SGS#0 Group Work Site 1033 False # TENANTADMIN#0 Tenant Admin Site 1033 False # Languages # Name Title # ---- ----- # German 1031 # English 1033 # French 1036 # Spanish 1034 # Set variables $SiteCollectionName = "Homepage" $SiteCollectionURL = "http://sharepoint.contoso.com" $SiteCollectionTemplate = "STS#0" $SiteCollectionLanguage = 1033 $SiteCollectionOwner = "contoso\UserName" # Create a new Sharepoint Site Collection New-SPSite -URL $SiteCollectionURL -OwnerAlias $SiteCollectionOwner -Language $SiteCollectionLanguage -Template $SiteCollectionTemplate -Name $SiteCollectionName
Creating a new Sharepoint WebApplication and a Sharepoint Site Collection:
# SharePoint cmdlets Add-PsSnapin Microsoft.SharePoint.PowerShell # Set variables $WebAppName = "Contoso Sharepoint1" $WebAppHostHeader = "sharepoint1.contoso.com" $WebAppPort = 80 $WebAppAppPool = "SharePoint1AppPool" # This User has to be a Sharepoint Manager Account $WebAppAppPoolAccount = "contoso\sp_serviceuser" $WebAppDatabaseName = "Sharepoint1" $WebAppDatabaseServer = "SQLServer\InstanceName" $SiteCollectionName = "Homepage" $SiteCollectionURL = ("http://" + $WebAppHostHeader) $SiteCollectionTemplate = "STS#0" $SiteCollectionLanguage = 1033 $SiteCollectionOwner = "contoso\UserName" # Create a new Sharepoint WebApplication New-SPWebApplication -Name $WebAppName -Port $WebAppPort -HostHeader $WebAppHostHeader -URL ("http://" + $WebAppHostHeader) -ApplicationPool $WebAppAppPool -ApplicationPoolAccount (Get-SPManagedAccount $WebAppAppPoolAccount) -DatabaseName $WebAppDatabaseName -DatabaseServer $WebAppDatabaseServer # Create a new Sharepoint Site Collection New-SPSite -URL $SiteCollectionURL -OwnerAlias $SiteCollectionOwner -Language $SiteCollectionLanguage -Template $SiteCollectionTemplate -Name $SiteCollectionName
Cool! Nice article, I will give it a try!
Good post, would like to know however how to create a site collection with an own dedicated database, so a site collection that does not use the web application’s database?
Hi i fallow the step by step , You declare WebAppHostHeader but not declare WebhostHeader . In command you are using both variables. so what we have to give to Webhostheader.
Thanks
kumar
Sorry for this mistake, should be WebAppHostHeader in both cases
Hi, great article.
I am getting a annoying error when i am trying to create a new site.
getting: A parameter cannot be found that matches parameter name ‘owneralias’
The code i am using is:
#set variables
$SiteName = “Test”
$SiteURL = “http://spserver/test”
$SiteTemplate = “STS#0”
$SiteOwner = “SPSERVER2010\SPAdmin”
#Create the new site
New-SPWeb -URL $SiteURL -OwnerAlias $SiteOwner -Template $SiteTemplate -Name $SiteName
Any clue why i get that error message?
Be careful, the command I used is New-SPWebApplication not just New-SPWeb. SPWeb is (i think so, have not tested that) is not the same as the SPWebApplication.
Your scripts look nice. But line breaks are removed when we copy them. was that intentional?
Great scripts Thomas! I am using them to create some large sites for testing. I did find one minor typo in the create web app script.
-URL (“http://” + $WWebAppHostHeader)
should be
-URL (“http://” + $WebAppHostHeader)
Thanks for saving me tons of time!
-G
Thanks I will correct the typo :)
Great post. Thanks for sharing your knowledge!