web
You’re offline. This is a read only version of the page.
close
Skip to main content
Community site session details

Community site session details

Session Id :
Dynamics 365 Community / Blogs / Nishant Rana’s Weblog / Sample PowerShell Script fo...

Sample PowerShell Script for creating and deleting WebApplication and SiteCollection in SharePoint 2010

Nishant Rana Profile Picture Nishant Rana 11,325 Microsoft Employee

Sample PowerShell Script for creating a web application in SharePoint 2010

# add SharePoint Powershell snapin

add-PSSnapin Microsoft.Sharepoint.powershell

#define variables 

$WebAppName=”SharePoint – 120″

$WebAppPort=120

$WebAppUrl=”http://servername:port/”

$WebAppPool=”SharePoint – 120″

$WebAppPoolAccount=”domain\password”

$WebAppDatabaseServer=”servername\SHAREPOINT”

$WebAppDatabaseName=”WSS_Content_120″ 

new-SPWebApplication -name $WebAppName -url $WebAppUrl -ApplicationPool $WebAppPool -ApplicationPoolAccount (Get-SPManagedAccount $WebAppPoolAccount) -DatabaseServer $WebAppDatabaseServer -DatabaseName $WebAppDatabaseName    

write-host “New Web Application successfully created”

write-host “—————————————-”

write-host “Application name ” $webAppName -foregroundcolor Green

 

Sample PowerShell Script for deleting a web application in SharePoint 2010

$WebAppUrl=”http:/servername:port”

write-host “Begin Deleting Web Application at ” $WebAppUrl

write-host

Remove-SPWebApplication -Identity $WebAppUrl

write-host “New wEb Application successfully deleted”

Sample PowerShell Script for Creating Site Collection in SharePoint 2010

if($args)

{

$sitename=$args[0]

$siteurl=”http://servername:port/sites/” + $SiteName

write-host “Begin Creating Site Collection at ” $SiteUrl

write-host

$NewSite = New-SPSite -Url $SiteUrl -OwnerAlias Administrator -Template STS#1 -Name $SiteName

$RootWeb=$NewSite.RootWeb 

$RootWeb.Title=”Scripted Site -” + $SiteName

$RootWeb.Update()

write-host “New Scripted Site successfull created”

write-host “Title: ” $RootWeb.Title -foregroundcolor Green

}

else{
write-host “Error : You must supply parameter for site name” -foregroundcolor Red

}
 For Deleting the SiteCollection

if($args)

{

$sitename=$args[0]

$siteurl=”http://servername:port/sites/” + $SiteName

write-host “Begin Deleting Site Collection at ” $SiteUrl

write-host

Remove-SPSite -Identity $siteurl

write-host “New Site successfully deleted”

}

else

{

write-host “Error : You must supply parameter for site name” -foregroundcolor Red

}

 

Save the script files with the extension .ps1

To run them

./filename.ps1

Few other important PowerShell command (Get-Help, Get-Location, Set-Location)


Filed under: SharePoint 2010 Tagged: SharePoint 2010

This was originally posted here.

Comments

*This post is locked for comments