Thursday, January 31, 2013

SharePoint 2010 PowerShell Scripts I Use Frequently


Get-SPSite

Returns all site collections that match the given criteria.

The Get-SPSite cmdlet returns either a single site that matches the Identity parameter, or all the sites that match the Filter parameter for the specified scope. The scopes are the WebApplication, ContentDatabase , and SiteSubscription parameters. If none of these scopes is provided, the scope is the farm. If the scope is specified with no Filter parameter, all sites in that scope are returned.

Get-SPSite [-AssignmentCollection <SPAssignmentCollection>] [-Confirm [<SwitchParameter>]] [-Filter <ScriptBlock>] [-Limit <String>] [-WebApplication <SPWebApplicationPipeBind>] [-WhatIf [<SwitchParameter>]]
Get-SPSite [-Identity] <SPSitePipeBind> [-AssignmentCollection <SPAssignmentCollection>] [-Confirm [<SwitchParameter>]] [-Filter <ScriptBlock>] [-Limit <String>] [-Regex <SwitchParameter>] [-WhatIf [<SwitchParameter>]]
Get-SPSite -ContentDatabase <SPContentDatabasePipeBind> [-AssignmentCollection <SPAssignmentCollection>] [-Confirm [<SwitchParameter>]] [-Filter <ScriptBlock>] [-Limit <String>] [-WhatIf [<SwitchParameter>]]
 Get-SPSite -SiteSubscription <SPSiteSubscriptionPipeBind> [-AssignmentCollection <SPAssignmentCollection>] [-Confirm [<SwitchParameter>]] [-Filter <ScriptBlock>] [-Limit <String>] [-WhatIf [<SwitchParameter>]]
 This example gets the collection of subweb titles in site collection at http://sitename.
Get-SPSite 'http://sitename' | Get-SPWeb -Limit All | Select Title
This example gets a subset of data from all of the sites in the content database b399a366-d899-4cff-8a9b-8c0594ee755f.
Get-SPSite -ContentDatabase "b399a366-d899-4cff-8a9b-8c0594ee755f" | Format-Table -Property Url, Owner, SecondaryOwner
My common usage:
List site sizes:
$site = Get-SPSite http://server/sites/site
$site.Usage.Storage / 1mb;
$site.Dispose();
 
 List Library sizes:
$site = Get-SPSite "http://server/sites/site";
$dataTable = $site.StorageManagementInformation(2,0x11,0,5);
$dataTable | ft Title, Size

Source: http://technet.microsoft.com/en-us/library/ff607950(v=office.14).aspx


Export-SPWeb

Exports a site, list, or library.

The Export-SPWeb cmdlet exports a site, list, or library. The capability to export from a library is a new feature in SharePoint 2010 Products.

Export-SPWeb [-Identity] <SPWebPipeBind> -Path <String> [-AssignmentCollection <SPAssignmentCollection>] [-CompressionSize <Int32>] [-Confirm [<SwitchParameter>]] [-Force <SwitchParameter>] [-HaltOnError <SwitchParameter>] [-HaltOnWarning <SwitchParameter>] [-IncludeUserSecurity <SwitchParameter>] [-IncludeVersions <LastMajor | CurrentVersion | LastMajorAndMinor | All>] [-ItemUrl <String>] [-NoFileCompression <SwitchParameter>] [-NoLogFile <SwitchParameter>] [-UseSqlSnapshot <SwitchParameter>] [-WhatIf [<SwitchParameter>]]
This example exports the site at http://site/ to a file called site export.cmp in the current directory.

Export-SPWeb http://server_name/sites/site_name –Path "site export.cmp"
My common usage:

Export-SPWeb -Identity http://server_name/sites/site_name -Path d:\import\im.cmp -IncludeUserSecurity -NoFileCompression -Verbose –Force
List sub-site sizes
function GetWebSizes ($StartWeb)
{
    $web = Get-SPWeb $StartWeb
    [long]$total = 0
    $total += GetWebSize -Web $web
    $total += GetSubWebSizes -Web $web
    $totalInMb = ($total/1024)/1024
    $totalInMb = "{0:N2}" -f $totalInMb
    $totalInGb = (($total/1024)/1024)/1024
    $totalInGb = "{0:N2}" -f $totalInGb
    write-host "Total size of all sites below" $StartWeb "is" $total "Bytes,"
    write-host "which is" $totalInMb "MB or" $totalInGb "GB"
    $web.Dispose()
}
function GetWebSize ($Web)
{
    [long]$subtotal = 0
    foreach ($folder in $Web.Folders)
    {
        $subtotal += GetFolderSize -Folder $folder
    }
    write-host "Site" $Web.Title "is" $subtotal "KB"
    return $subtotal
}
function GetSubWebSizes ($Web)
{
    [long]$subtotal = 0
    foreach ($subweb in $Web.GetSubwebsForCurrentUser())
    {
        [long]$webtotal = 0
        foreach ($folder in $subweb.Folders)
        {
            $webtotal += GetFolderSize -Folder $folder
        }
        write-host "Site" $subweb.Title "is" $webtotal "Bytes"
        $subtotal += $webtotal
        $subtotal += GetSubWebSizes -Web $subweb
    }
    return $subtotal
}
function GetFolderSize ($Folder)
{
    [long]$folderSize = 0 
    foreach ($file in $Folder.Files)
    {
        $folderSize += $file.Length;
    }
    foreach ($fd in $Folder.SubFolders)
    {
        $folderSize += GetFolderSize -Folder $fd
    }
    return $folderSize
}
GetWebSizes -StartWeb https://test-2010clientportal.cbre.com/sites/im



Restore-SPDeletedSite


Restores a deleted site collection.

Use the Restore-SPDeletedSite cmdlet to restore a previously deleted site collection.
Unlike the Restore-SPSite cmdlet that uses the host name and scheme for the Identity parameter (that is, http://server_name), the value of the identity parameter for all SPDeletedSite cmdlets use a server-relative URL. Typically, the forward slash character (/) begins the relative URL and also denotes the root site.
Restore-SPDeletedSite [-Identity] <SPDeletedSitePipeBind> [-AssignmentCollection <SPAssignmentCollection>] [-Confirm [<SwitchParameter>]] [-ContentDatabase <SPContentDatabasePipeBind>] [-WebApplication <SPWebApplicationPipeBind>] [-WhatIf [<SwitchParameter>]]
 This example restores a specific deleted site collection by using the site ID.

Restore-SPDeletedSite -Identity 610857cb-8414-4a89-8bf3-ad3628f6c86c
Source: http://technet.microsoft.com/en-us/library/hh286319(v=office.14).aspx


Import-SPWeb

Imports a web, list, or library.

The Import-SPWeb cmdlet imports a web, list, or library. The capability to import from a library is a new feature in SharePoint 2010 Products.

Import-SPWeb [-Identity] <SPWebPipeBind> -Path <String> [-ActivateSolutions <SwitchParameter>] [-AssignmentCollection <SPAssignmentCollection>] [-Confirm [<SwitchParameter>]] [-Force <SwitchParameter>] [-HaltOnError <SwitchParameter>] [-HaltOnWarning <SwitchParameter>] [-IncludeUserCustomAction <None | All>] [-IncludeUserSecurity <SwitchParameter>] [-NoFileCompression <SwitchParameter>] [-NoLogFile <SwitchParameter>] [-UpdateVersions <Append | Overwrite | Ignore>] [-WhatIf [<SwitchParameter>]]

This example imports the contents of export.cmp into a site at http://site, overwriting the versioned content on the site with the contents of the export.cmp file:

Import-SPWeb http://server_name/sites/site_name –Path export.cmp –UpdateVersions –Overwrite
My common usage:
Import-SPWeb -Identity http://server_name/sites/site_name -Path d:\Import\site.cmp -IncludeUserSecurity -NoFileCompression -Verbose –Force


Restore-SPSite

Restores a site collection.

The Restore-SPSite cmdlet performs a restoration of the site collection to a location specified by the Identity parameter. A content database may only contain one copy of a site collection. If a site collection is backed up and restored to a different URL location within the same Web application, an additional content database must be available to hold the restored copy of the site collection.

Restore-SPSite [-Identity] <String> -Path <String> [-AssignmentCollection <SPAssignmentCollection>] [-Confirm [<SwitchParameter>]] [-ContentDatabase <SPContentDatabasePipeBind>] [-Force <SwitchParameter>] [-GradualDelete <SwitchParameter>] [-HostHeaderWebApplication <String>] [-WhatIf [<SwitchParameter>]]
Restore-SPSite [-Identity] <String> -Path <String> [-AssignmentCollection <SPAssignmentCollection>] [-Confirm [<SwitchParameter>]] [-DatabaseName <String>] [-DatabaseServer <String>] [-Force <SwitchParameter>] [-GradualDelete <SwitchParameter>] [-HostHeaderWebApplication <String>] [-WhatIf [<SwitchParameter>]]
This example restores a site collection from the backup file C:\Backup\site_name.bak to the site collection URL http://server_name/sites/site_name.

Restore-SPSite http://server_name/sites/site_name -Path C:\Backup\site_name.bak
This example restores a site collection backup from the backup file C:\Backup\site_name.bak, but overwrites the existing site collection at http://server_name/sites/site_name while specifying that the site collection must be stored in a specific content database.

Restore-SPSite http://server_name/sites/site_name -Path C:\Backup\site_name.bak -Force -DatabaseServer SQLBE1 -DatabaseName SQLDB1
This example restores a site collection backup from the backup file \\file_server\share\site_name.bak to the host-named site collection http://www.example.com on the Web application http://server_name.

Restore-SPSite http://www.example.com -Path \\file_server\share\site_name.bak -HostHeaderWebApplication http://server_name
My common usage:

Restore-SPSite -Identity http://server_name/sites/site_name -Path d:\backup\im.bak -DatabaseServer <SQLServer> -DatabaseName WSS_Content_ClientPortal4_EXTProd -Force -Verbose


Move-SPSite

Moves site collections from one content database to another.

The Move-SPSite cmdlet moves the data in the specified site collection from its current content database to the content database specified by the DestinationDatabase parameter.

Move-SPSite [-Identity] <SPSitePipeBind> -DestinationDatabase <SPContentDatabasePipeBind> [-AssignmentCollection <SPAssignmentCollection>] [-Confirm [<SwitchParameter>]] [-RbsProviderMapping <Hashtable>] [-WhatIf [<SwitchParameter>]]
This example moves the site collection http://servername/sites/sitename to the content database ContentDb2.

Move-SPSite http://servername/sites/sitename -DestinationDatabase ContentDb2
This example moves all site collections in ContentDb1 to ContentDb2.

Get-SPSite -ContentDatabase ContentDb1 | Move-SPSite -DestinationDatabase ContentDb2
This example moves all site collections where DOMAIN\username is the site collection owner to ContentDb2. The Get-SPSiteAdministration cmdlet is used instead of the Get-SPSite cmdlet because you must have permission within the site collection to access the properties of the SPSite object. You can access the properties of the SPSiteAdministration object as a SharePoint farm administrator.

Get-SPSiteAdministration | where { $_.OwnerLoginName -eq "DOMAIN\username" } | Move-SPSite -DestinationDatabase ContentDb2
My common usage:

Move-SPSite https://clientportal.cbre.com/Sites/IM -DestinationDatabase WSS_Content_ClientPortal11-EXT
 Source: http://technet.microsoft.com/en-us/library/ff607915.aspx



Get-SPDatabase

Retrieves all properties of a database.

The Get-SPDatabase cmdlet displays all public properties of a database to the current window. If the Identity parameter is specified, only properties of that ID are displayed.

 Get-SPDatabase [[-Identity] <SPDatabasePipeBind>] [-AssignmentCollection <SPAssignmentCollection>]

Get-SPDatabase -ServerInstance <SPDatabaseServiceInstancePipeBind> [-AssignmentCollection <SPAssignmentCollection>]
This example gets all databases on a specific named SQL Server Express instance.

Get-SPDatabase –ServerInstance "ServerA\SharePoint"
My common usage:
Get-SPDatabase | Sort-Object disksizerequired -desc | Format-Table Name, @{Label ="Size in MB"; Expression = {$_.disksizerequired/1024/1024}}
Source: http://technet.microsoft.com/en-us/library/ff607889(v=office.14).aspx



Get-SPUser

Returns the user account or accounts that match a given search criteria.

The Get-SPUser cmdlet returns all SharePoint user accounts that match the scope given by the Identity, Web, or Group parameters.
Get-SPUser [[-Identity] <SPUserPipeBind>] -Web <SPWebPipeBind> [-AssignmentCollection <SPAssignmentCollection>] [-Group <SPGroupPipeBind>] [-Limit <String>]
This example returns all members of the group Viewers on the Web site http://zsharepoint2.
Get-SPUser -Web "http://zsharepoint2" -Group "Viewers"
Source: http://technet.microsoft.com/en-us/library/ff607580(v=office.14).aspx

No comments:

Post a Comment