Hey, I’m back with couple of PowerShell CmdLets for SharePoint 2010.
Save any of the following as .ps1 file and execute them in the PowerShell command prompt.
- Get the list of web templates
get-spwebtemplate | sort-object "name"
- Create a site collection
Add-PSSnapin Microsoft.SharePoint.PowerShell –ErrorAction SilentlyContinue
$server = Read-Host “Enter SQL Server”
$dbname = Read-Host “Enter Database Name”
$webapp = Read-Host “Enter Web Application URL”
$site = Read-Host “Enter New Site Collection URL”
$sitecollectionname = Read-Host "Enter Title of the Site collection"
$sitedesc = Read-Host "Enter Description of the Site collection"
$owner1 = Read-Host “Enter Primary Site Collection Admin”
# $owner2 = Read-Host “Enter Secondary Site Collection Admin”
# $sitetemplate = Read-Host "Enter Template name for the Site Collection"
New-SPContentDatabase -Name $dbname -DatabaseServer $server -WebApplication $webapp | out-null
New-SPSite -URL $site -OwnerAlias $owner1 -ContentDatabase $dbname -Name $sitecollectionname -Description $sitedesc -Template STS#0 | out-null
Get-SPContentDatabase -Site $site | Set-SPContentDatabase -MaxSiteCount 15000 -WarningSiteCount 9000
Write-Host ” “
Write-Host “Site Collection at” $site “has been created in the” $dbname “content database” -ForegroundColor Yellow
#-SecondaryOwnerAlias $owner2 - Add this to SPSite to add Secondary SCA
- Restore a site collection
Add-PSSnapin Microsoft.SharePoint.PowerShell –ErrorAction SilentlyContinue
$server = Read-Host “Enter SQL Server”
$dbname = Read-Host “Enter Database Name”
$site = Read-Host “Enter New Site Collection URL”
$path = Read-Host "Enter backup path location"
Restore-SPSite -Identity $site -Path $path -DatabaseServer $server -DatabaseName $dbname -Force -Verbose
Write-Host ""
Write-Host "Site collection " $site " has been restored properly in the” $dbname “content database” -ForegroundColor Yellow
- Moving a Site collection from one content database to another content database
$siteUrl = Read-Host “Enter Site Collection URL to be moved”
$newDBName = Read-Host “Enter destination Database Name”
$contentDBInfo = Get-SPContentDatabase -Site $siteUrl
$oldDBName = $contentDBInfo.Name
Move-SPSite -Identity $siteUrl -DestinationDatabase $newDBName
Write-Host ""
Write-Host "Site collection " $siteUrl "has been moved from " $oldDBName "to " $newDBName " successfully." -ForegroundColor Blue
- Create a content database for a web app
Add-PSSnapin Microsoft.SharePoint.PowerShell –ErrorAction SilentlyContinue
$server = Read-Host “Enter SQL Server”
$dbname = Read-Host “Enter Database Name”
$webapp = Read-Host “Enter Web Application URL”
New-SPContentDatabase -Name $dbname -DatabaseServer $server -WebApplication $webapp | out-null
- Removing content database from FARM and DB
Remove-SPContentDatabase -Identity <Content DB Guid>
Alternatively you can use the below one:
$dbname = Read-Host “Enter Database Name”
get-SPContentDatabase -Identity $dbname | Remove-SPContentDatabase
Write-Host ""
Write-Host "The content dabase" $dbname "has been deleted successfully." -ForegroundColor Blue
- Exporting a Site – While exporting the site contents, it would also retain the metadata like created date, modified date, author name, …..
$siteUrl = Read-Host "Enter the Source Site Url"
$path = Read-Host "Enter the destination file path"
Export-SPWeb -Identity $siteUrl -Path $path -IncludeVersions All -IncludeUserSecurity -Verbose
Write-Host ""
Write-Host "The site" $siteUrl "content has been exported from" $path "successfully" -ForegroundColor Blue
- Importing a Site – While importing the site contents, it would also retain the metadata like created date, modified date, author name, …..
$siteUrl = Read-Host "Enter the Destination Site Url"
$path = Read-Host "Enter the backup file path"
Import-SPWeb -Identity $siteUrl -Path $path -UpdateVersions Overwrite -IncludeUserSecurity -Verbose
Write-Host ""
Write-Host "The site" $siteUrl "content has been imported from" $path "successfully" -ForegroundColor Blue
Regards
Gangadhar Kotu