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:
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:
We can check the list of installed snap-ins by using this command:
Connecting to SharePoint Remotely
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, enter the following command:
PowerShell Permissions
following command:
Add-SPShellAdmin -Username domain\username -database (Get-SPContentDatabase-webapplication http://weburl)
Working with Site Collections and Sites
When it comes to managing site objects (SPWeb), we can pick up a specific web site using this:
or
Creating Site Collections and Sites
Create a new site collection using the New-SPSite cmdlet:
Add new sites using the New-SPWeb cmdlet:
Deleting Site Collections and Sites
We can delete site collections and sites by using the Remove-SPSite or the Remove-SPWeb cmdlet.
or
Setting Properties on SharePoint Objects
$web=SP-GetSPWeb -Identity http://myweburl
$web.Title="My New Title"
$web.Update()
Working with Lists and Libraries
Enumerate the lists on a site using the following:
Get-SPWeb -Identity http://myweburl | Select -Expand lists| Select Title
Add new lists using the Add method of the Lists property:
Get-SPWeb -Identity http://myweburl | ForEach {$_.Lists.Add("My Task List", "",$_.ListTemplates["Tasks"])}
Working with Content
Retrieve a list of all items in a site using the following:
Apply a filter to show only documents:
Use of filters to search for a specific item:
Get-SPWeb -Identity http://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 "http://myweburl" -ListName "Shared Documents" -DocumentName "PowerShellDocument.txt" -Content "Document Content"
Working with Timer Jobs
Get a list of all timer jobs:
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
No comments:
Post a Comment