Posts Tagged ‘PowerShell’

SharePoint 2010 PowerShell CmdLets

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.

  1. Get the list of web templates
  2. get-spwebtemplate | sort-object "name"

  3. Create a site collection
  4. 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

  5. Restore a site collection
  6. 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

  7. Moving a Site collection from one content database to another content database
  8. $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

  9. Create a content database for a web app
  10. 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

  11. Removing content database from FARM and DB
  12. 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

  13. Exporting a Site – While exporting the site contents, it would also retain the metadata like created date, modified date, author name, …..
  14. $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

  15. Importing a Site – While importing the site contents, it would also retain the metadata like created date, modified date, author name, …..
  16. $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

    Advertisements