Skip to main content

30 Powershell Commands Useful For Every SharePoint Developer

I have been working on SharePoint since long time and I find the PowerShell Cmdlets to be the most useful feature of SharePoint. PowerShell Cmdlets  is the most useful stuff for every SharePoint Developer and also Administrators for configuration and provisioning applications and services.

There must be more than 500+ such cmdlets available, but I would like to share some of the cmdlets that are useful day-to-day to SharePoint Developers.

ImgSrc: Mart's Sitecore Art

How to use the PowerShell Cmdlets?
There are two ways to use these PowerShell Cmdlets:
1) From the SP2010 Management Shell
 This opens a PowerShell Console session with all the cmdlets loaded.

2) Adding the Microsoft.SharePoint.PowerShell snap-in
Open a new PowerShell window, add the Microsoft.SharePoint.PowerShell snap-in to start using the cmdlets.

The Cmdlets
  1. Add Solution
    Add-SPSolution <literal path of wsp file>                                                                          
  2. Deploy Solution
    Install-SPSolution -Identity <solution_file (WSP Name)> -GACDeployment
  3. Uninstall Solution
    Uninstall-SPSolution –Identity "solution_file (WSP Name)".wsp –WebApplication "Site URL"
  4. Update/Upgrade Solution
    The Update-SPSolution cmdlet upgrades a deployed SharePoint solution in the farm. Use this cmdlet only if a new solution contains the same set of files and features as the deployed solution. If files and features are different, the solution must be retracted and redeployed by using the Uninstall-SPSolution and Install-SPSolution cmdlets, respectively
    Update-SPSolution -Identity "solution_file (WSP Name)" -LiteralPath <literal path of wsp file> -GACDeployment
  5. Install Feature
    Install-SPFeature -path <literal path to the 15\Template\Features directory.> -CompatibilityLevel 15
  6. Uninstall Feature
    Uninstall-SPFeature -Identity <Feature Name>
  7. Enable Feature
    Enable-SPFeature -identity <Feature Guid> -URL "Site URL"
  8. Get the list of features from site sorted by ID
    get-spfeature -site "Site URL" | Sort Id
  9. Get the list of features from site sorted by Name
    get-spfeature -site "Site URL" | Sort Displayname
  10. Get the list of features from site sorted by ID
    get-spfeature -site "Site URL" | Sort Id
  11. Get the list of features from site sorted by Name
    get-spfeature -site "Site URL" | Sort Displayname
  12. Take Site Backup
    backup-spsite -identity <Site URL> -path "literal path to save backup file"
  13. Restore Site Backup
    Restore-SPSite -Identity <Site URL> -Path <literal path to save Backup file> [-DatabaseServer <Database server name>] [-DatabaseName <Content database name>] [-HostHeader <Host header>] [-Force] [-GradualDelete] [-Verbose]
  14. Synchronise the AD User Data with User Profiles in SP Site
    Get-SPUser –Web http://SharePointServer | Set-SPUser –SyncFromAD
  15. Backing Up an Entire Web Application
    backup-spfarm –BackupMethod Full –Directory C:\Backup\ -Item http://server
  16. Backing Up the Farm Configuration
    backup-spfarm –BackupMethod Full –Directory C:\Backup\ -ConfigurationOnly
  17. Create a Web
    New-SPWeb –URL "Web URL" -Verbose
  18. Export a Web
    export-spweb -identity "Web URL" -path "<literal path>\<backup file name>.cmp"
  19. Import a Web
    Import-SPWeb -Identity "Web URL" -Path "<literal path>\<backup file name>.cmp" -IncludeUserSecurity -UpdateVersions Overwrite
  20. Export a List
    export-spweb -identity "Web URL" -path "<literal path>\<backup file name>.cmp" -itemurl "/<listname>"
  21. Import a List
    Import-SPWeb -Identity "Web URL" -Path "<literal path>\<backup file name>.cmp" -IncludeUserSecurity -UpdateVersions Overwrite
  22. Find List Name from Id
    Get-SPSite "Site URL" | Get-SPWeb  -Limit ALL | %{$_.Lists} | ?{$_.ID –eq "<List ID>"} | ft Title, ParentWebURL, RootFolder
  23. Assign User as Site Collection Administrator
    Get-SPSite -Identity "Site URL" | %{Set-SPSite $_ -OwnerAlias "username" -SecondaryOwnerAlias "username"}
  24. Get list of deployed solutions
    $SPfarm = [Microsoft.SharePoint.Administration.SPFarm]::get_Local()
  25. Download the WSP that you have deployed in a SharePoint farm
    $frm = Get-SPFarm
    $file = $frm.Solutions.Item("Name of Solution i.e.WSP file name").SolutionFile
    $file.SaveAs("literal path to save wsp file")
  26. Get User Info
    Get-SPUser -Web "Site URL" |  Where { $_.UserLogin -LIKE "*|domain\username" } |  Select ID, UserLogin, DisplayName
  27. Get SharePoint Groups from Site Collection
    Get-SPSite "Site URL" |
      Select -ExpandProperty RootWeb | Select -ExpandProperty Groups |  Select {$_.ParentWeb.Url}, Name
  28. Get All Users from SharePoint Group
    Get-SPSite "Site URL" |
      Select -ExpandProperty RootWeb |  Select -ExpandProperty Groups |  Where {$_.Name -EQ "<Group Name>"} |  Select -ExpandProperty Users |  Select Name, Email
  29. Get SharePoint Groups of given user$user = Get-SPUser -Web "Site URL" |  Where {$_.LoginName -LIKE "*|domain\username"}
  30. Get All Site Owners and All Site Collection Administrators
    Get All Site Owners:
    $web = Get-SPWeb "Site URL"
    Get  All Site Collection Administrators:
    Get-SPSite -Limit All | Select Url, Owner, SecondaryContact

    Doing such stuffs 
    in the SharePoint UI, takes at least 5-10 minutes, but with PowerShell it takes only seconds!

    I will post some more cmdlets from administration and configuration point of view in some future post. Till then enjoy using PowerShell Cmdlets 🙂