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

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)


Author: Nishant Rana

I love working in and sharing everything about Microsoft.NET technology !

4 thoughts on “Sample PowerShell Script for creating and deleting WebApplication and SiteCollection in SharePoint 2010”

  1. Hello would you mind letting me know which hosting company
    you’re utilizing? I’ve loaded your blog in 3 completely different
    internet browsers and I must say this blog loads a
    lot quicker then most. Can you recommend a good internet hosting provider at a honest price?
    Thank you, I appreciate it!

    Like

Share your thoughts

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