Condividi tramite


Some useful commands in PowerShell

In previous versions of SharePoint, command line configuration was available via the STSADM tool. Although the tool is still available for use with SharePoint 2010, the recommended approach for managing a SharePoint farm via the command line is to use the new SharePoint 2010 Management Shell.

This shell is a PowerShell instance with a collection of SharePoint-specific cmdlets (pronounced command-lets) installed. In this, we’ll take a look at the few of the main cmdlets.

First things first. Where can we find PowerShell?

When running on a SharePoint server, two possibilities exist: either select the SharePoint 2010 Management Shell from the Start menu.

 or

 open a command prompt and enter the following: "PowerShell". 

If we’re using the SharePoint management shell, the SharePoint snap-in will already be installed. If we’re using a standard PowerShell console, we can install the snap-in by entering the following command: "Add-PSSnapIn Microsoft.SharePoint.PowerShell"

We can check the list of installed snap-ins by using this command: "Get-PSSnapIn"

Some Basic Commands

1.Connecting to SharePoint Remotely

One of the real benefits of PowerShell is its ability to connect to remote machines. We can open a PowerShell session on a client machine and then use remoting to connect to a SharePoint server. To enable remoting on the server, entert he following command:

Enable-PSRemoting

This command will enable the WinRM service and set up the firewall to allow incoming sessions.

After the server has been configured, we can connect from any client machine by entering the following command:

Enter-PSSession "Server Name" -Credential
(Get-Credential)

NOTE: If the client machine is running on a domain and your SharePoint server is running as a standalone server, a few other steps are necessary to enable remote connectivity, such as configuring Secure Sockets Layer (SSL) connectivity on the server.

After a remote connection has been established, the SharePoint snap-in can be added with the command:

Add-PSSnapin Microsoft.SharePoint.PowerShell

 

2.PowerShell Permissions

To use SharePoint cmdlets, a user must be a member of the SharePoint_Shell_Access role for the farm configuration database as well as a member of the WSS_ADMIN_WPG group on the SharePoint front-end server. To grant users the appropriate permissions, use the following command:

Add-SPShellAdmin -Username domain\username -database
(Get-SPContentDatabase -> -webapplication)

Each user must be explicitly granted permissions to every database to which he or she needs access. By default, only the account used to sett up SharePoint will have permission to execute this command.

 

3.Working with Site Collections and Sites

Most of the cmdlets commonly used in the management of site collections or sites end in SPSite or SPWeb. To pick
up a reference to a site collection, we can use the following:

 $site=Get-SPSite -Identity 

Or
we can return a list of all site collections by using this:

 Get-SPSite

When it comes to managing site objects (SPWeb), we can pick up a specific web site using this:

 $web=Get-SPWeb -Identity 

To return a list of sites, we need to use either the Site parameter or an SPSite object:

 Get-SPWeb -Site

or

 Get-SPWeb -Site $site

Creating Site Collections and Sites

We can create a new site collection using the New-SPSite cmdlet:

 New-SPSite -Url https://localhost/Sites/NewSiteCollection- OwnerAlias username

We can also add new sites using the New-SPWeb cmdlet:

 New-SPWeb -Url https://localhost/Sites/NewSiteCollection/NewWeb -Name MyNewWeb

Deleting Site Collections and Sites

We can delete site collections and sites by using the Remove-SPSite or the Remove-SPWeb cmdlet.

 Remove-SPWeb -Identity https://localhost/Sites/NewSiteCollection/NewWeb

or

 Remove-SPSite -Identity https://localhost/Sites/NewSiteCollection

Setting Properties on SharePoint Objects

When setting properties on the objects returned by SharePoint management cmdlets, we need to call the Update method in the same manner as when updating properties using the Server Object Model. Here’s an example:

 $web=SP-GetSPWeb -Identity https://myweburl
 $web.Title="My New Title"

$web.Update()
  

4.Working with Lists and Libraries

Similarly to how lists and libraries are accessed in the Server Object Model, they can be accessed via SPWeb objects. For example, we can enumerate the lists on a site using the following:

 Get-SPWeb -Identity | Select -Expand lists| Select Title

We can add new lists using the Add method of the Lists property:

 Get-SPWeb -Identity | ForEach {$_.Lists.Add("My Task List", "", $_.ListTemplates["Tasks"])}

Changing the Business Connectivity Thresholds

The maximum number of rows that can be retrieved via a Business Connectivity Services (BCS) connection is limited. The only way to change this value is via PowerShell. We can use the following command to retrieve the current settings:

 $proxies=Get-SPServiceApplicationProxy | Where {$_.TypeName -like "Business Data*"} $rule=Get-SPBusinessDataCatalogThrottleConfig -ServiceApplicationProxy $proxies -Scope -> Database -ThrottleType Items $rule

We can then update the value using the following:

 Set-SPBusinessDataCatalogThrottleConfig -Identity $rule -Maximum 10000  Default 10000
  

5.Working with Content

We can retrieve a list of all items in a site using the following:

 Get-SPWeb -Identity | Select -Expand Lists | Select -Expand Items |->select Name, Url

Or

we can apply a filter to show only documents:

 Get-SPWeb -Identity | Select -Expand Lists | Where {$_.BaseType -eq -> "DocumentLibrary"} | Select -Expand Items | select Name, Url

We  can also make use of filters to search for a specific item:

 Get-SPWeb -Identity https://myweburl | Select -Expand Lists | Select -Expand Items | -> Where {$_.Name -like "foo*"} | select Name, Url

Creating New Documents

To create a new document in a document library, use the following:

 function New-SPFile($WebUrl, $ListName, $DocumentName,$Content)
 {
 $stream = new-object System.IO.MemoryStream
 $writer = new-object System.IO.StreamWriter($stream)
 $writer.Write($content)
 $writer.Flush()

$list=(Get-SPWeb  $WebUrl).Lists.TryGetList($ListName)
 $file=$list.RootFolder.Files.Add($DocumentName, $stream,$true)
 $file.Update()
 }

New-SPFile -WebUrl "" -ListName "Shared Documents" -DocumentName ->

"PowerShellDocument.txt" -Content "Document Content"
  

6.Working with Timer Jobs

SharePoint makes use of timer jobs to perform a lot of back-end processing. We can use PowerShell to get a list of all timer jobs:

Get-SPTimerJob

Or
we can get a list of job failures grouped by the job name:

 Get-SPTimerJob | Select -Expand HistoryEntries | Where {$_.Status -ne "Succeeded"} -> | group JobDefinitionTitle

Comments

  • Anonymous
    March 08, 2012
    Very useful . Thanks Kunal.