SharePoint 2010 ships with some 531 PowerShell cmdlets giving
administrators ultimate power over their SharePoint server. I wish this
post will help you get started with it.
SharePoint PowerShell “snapin” is installed on any server on which you performed installation of SharePoint 2010 front end server or application server
Getting Help
Get help for a cmdlet:
Get-Help Get-SPSite
Get a list of all SharePoint cmdlets:
Get-Command -Module Microsoft.SharePoint.PowerShell
Wildcard search for a cmdlet:
Get-Command *Backup*
Finding out which properties and methods an object emitted by a cmdlet has:
Get-SPWeb | Get-Member
Permissions
To grant these permissions sufficient to use PowerShell for SharePoint 2010,use Add-SPShellAdmin cmdlet and specify the user and the databases to which the user needs access, e.g.
Add-SPShellAdmin -UserName contoso\velaskec -database
(Get-SPContentDatabase -webapplication http://sitename)
try {
Write-Output “Setting permissions” | Out-File $logFile -append
New-SPUser -UserAlias ($comp.Name + “\User1″) -Web $siteUrl -SiteCollectionAdmin
New-SPUser -UserAlias ($comp.Name + “\User2″) -Web $siteUrl -Group “Home Owners”
New-SPUser -UserAlias ($comp.Name + “\User3″) -Web $siteUrl -Group “Home Members”
New-SPUser -UserAlias ($comp.Name + “\User4″) -Web $siteUrl -Group “Home Visitors”
} catch [Exception] { Write-OutPut $_.Exception.ToString() | Out-File $logFile -append}
To Create a new WebApplication
try
{
$comp = Get-WmiObject -Class Win32_ComputerSystem
$hostHeader = “mywebsite.com”
$port = 80
$siteUrl = “http://” + $hostHeader
$appPoolName = “SharePoint – ” + $hostHeader + $port
$solPath = (Get-ChildItem (“..\bin\Debug\” + $solName)).FullName
$ownerAccount = [System.Security.Principal.WindowsIdentity]::GetCurrent().Name
$logFile = $workFolder + “\ReinstallPersonalDev.log”
$appName = “My Web Site”
$appPoolAccount = “domain\username “
$databaseName = “Mywebsite_Content_” + $comp.Name
$databaseServer = “MyDatabase”
} catch [Exception] { Write-OutPut $_.Exception.ToString() | Out-File $logFile -append}
Delete WebApplication
try {
Write-Output “Deleting web application” | Out-File $logFile -append
Remove-SPWebApplication -Identity $siteUrl -DeleteIISSite:$true -RemoveContentDatabases:$true -Confirm:$false | Out-File $logFile –append
Start-Sleep -Seconds $deploySleepSecs | Out-File $logFile -append
} catch [Exception] { Write-OutPut $_.Exception.ToString() | Out-File $logFile -append}
Recycling app pool
try {
Write-Output “Recycling app pool” | Out-File $logFile -append
$appPool = Get-WMIObject -NameSpace “root\MicrosoftIISv2″ -Class “IIsApplicationPool” | Where-Object {$_.Name -eq “W3SVC/APPPOOLS/$appPoolName”}
$appPool.Recycle() | Out-File $logFile -append
} catch [Exception] { Write-OutPut $_.Exception.ToString() | Out-File $logFile -append}
To create a new SiteCollection
try {
Write-Output “Creating site collection” | Out-File $logFile -append
New-SPSite -Url $siteUrl -OwnerAlias $ownerAccount -Name $rootTitle -Template $rootTempl -Confirm:$false
Start-Sleep -Seconds $deploySleepSecs | Out-File $logFile -append
} catch [Exception] { Write-OutPut $_.Exception.ToString() | Out-File $logFile -append}
To get the number of site collections:
(Get-SPSite).Count
To remove all site collections without confirmations:
Get-SPSite | Remove-SPSite -Confirm:$false
To Delete a SiteCollection
try {
Write-Output “Deleting site collection” | Out-File $logFile -append
Remove-SPSite -Identity $siteUrl -Confirm:$false | Out-File $logFile -append
} catch [Exception] { Write-OutPut $_.Exception.ToString() | Out-File $logFile -append}
To create new site:
try {
Write-Output “Creating first-level sites” | Out-File $logFile -append
New-SPWeb -Url ($siteUrl + “/” + $firstSiteUrl) -Name $ firstSiteTitle -Template $templName
Start-Sleep -Seconds $deploySleepSecs | Out-File $logFile -append
}catch [Exception] { Write-OutPut $_.Exception.ToString() | Out-File $logFile -append}
To create new task list in all sites:
Get-SPWeb | ForEach {$_.Lists.Add(“My Tasks”, “”,$_.ListTemplates["Tasks"])}
To create new task list in site:
Get-SPWeb $url | ForEach {$_.Lists.Add(“My Tasks”, “”,$_.ListTemplates["Tasks"])}
To enumerate available workflows:
Get-SPWeb $url | Select -Expand WorkflowTemplates | Select Name
To enumerate all document libraries in your site:
Get-SPWeb $url | Select -Expand Lists | Where {$_.BaseType -eq “DocumentLibrary”}
Adding solutions
$solPath = (Get-ChildItem (“..\bin\Debug\” + $solName)).FullName
try {
Write-Output “Adding solutions” | Out-File $logFile -append
Add-SPSolution -LiteralPath $solPath -Confirm:$false | Out-File $logFile -append
Start-Sleep -Seconds $deploySleepSecs | Out-File $logFile -append
} catch [Exception] { Write-OutPut $_.Exception.ToString() | Out-File $logFile -append}
Installing solutions
try {
Write-Output “Installing solutions” | Out-File $logFile -append
####TODO: use line below after we have webapp-scoped items in solution, now must be deployed globally
#Install-SPSolution -Identity $solName -WebApplication $siteUrl -GACDeployment -Confirm:$false -force
Install-SPSolution -Identity $solName -GACDeployment -Confirm:$false -force | Out-File $logFile -append
Start-Sleep -Seconds $deploySleepSecs | Out-File $logFile -append
} catch [Exception] { Write-OutPut $_.Exception.ToString() | Out-File $logFile -append}
Uninstalling solutions
try {
Write-Output “Uninstalling solutions” | Out-File $logFile -append
####TODO: use line below after we have webapp-scoped items in solution, now must be deployed globally
#Uninstall-SPSolution -Identity $solName -WebApplication $siteUrl -Confirm:$false | Out-File $logFile -append
Uninstall-SPSolution -Identity $solName -Confirm:$false | Out-File $logFile -append
Start-Sleep -Seconds $deploySleepSecs | Out-File $logFile -append
} catch [Exception] { Write-OutPut $_.Exception.ToString() | Out-File $logFile -append}
Removing solutions
try {
Write-Output “Removing solutions” | Out-File $logFile -append
Remove-SPSolution -Identity $solName -Confirm:$false | Out-File $logFile -append
Start-Sleep -Seconds $deploySleepSecs | Out-File $logFile -append
} catch [Exception] { Write-OutPut $_.Exception.ToString() | Out-File $logFile -append}
Activating Feature
try {
Write-Output “Activating feature” | Out-File $logFile -append
Enable-SPFeature -Identity featureName -Url $siteUrl -Confirm:$false
} catch [Exception] { Write-OutPut $_.Exception.ToString() | Out-File $logFile -append}
Working with Content
Show all items in a site:
Get-SPWeb $url | Select -Expand Lists | Select -Expand Items | select
Name, Url
Show only documents:
Get-SPWeb $url | Select -Expand Lists | Where {$_.BaseType -eq
“DocumentLibrary”} | Select -Expand Items | select Name, Url
Search for item:
Get-SPWeb $url | Select -Expand Lists | Select -Expand Items | Where
{$_.Name -like “*.doc”} | select Name, Url
To create a new document in a document library:
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()
Get-SPWeb $WebUrl | ForEach {$_.Lists[$ListName]} | ForEach
{$_.RootFolder.Files.Add($DocumentName, $stream,
$true);$_.Update()}
}
New-SPFile -WebUrl “http://mycompany/sites/mysite” -ListName
“Shared Documents” -DocumentName “MyFirstDocument” -Content
“Power Blues”
Recycle Bin
To find an item by its name in the Recycle Bin for a site:
(Get-SPWeb “http://sp2010dc” ).RecycleBin | Where {$_.Title -match “cool”}
Then use the item ID to restore it:
(Get-SPWeb “http://sp2010dc” ).RecycleBin.Restore( -”b23d2d41-cd6a-4471-a89 1 -c86f83563e11″ )
For Site Collection Recycle Bin use:
(Get-SPSite).RecycleBin
SharePoint PowerShell “snapin” is installed on any server on which you performed installation of SharePoint 2010 front end server or application server
- On the Start menu, click SharePoint 2010 Management Shell,
- Or in a regular PowerShell session, execute: Add-PSSnapin
Getting Help
Get help for a cmdlet:
Get-Help Get-SPSite
Get a list of all SharePoint cmdlets:
Get-Command -Module Microsoft.SharePoint.PowerShell
Wildcard search for a cmdlet:
Get-Command *Backup*
Finding out which properties and methods an object emitted by a cmdlet has:
Get-SPWeb | Get-Member
Permissions
To grant these permissions sufficient to use PowerShell for SharePoint 2010,use Add-SPShellAdmin cmdlet and specify the user and the databases to which the user needs access, e.g.
Add-SPShellAdmin -UserName contoso\velaskec -database
(Get-SPContentDatabase -webapplication http://sitename)
try {
Write-Output “Setting permissions” | Out-File $logFile -append
New-SPUser -UserAlias ($comp.Name + “\User1″) -Web $siteUrl -SiteCollectionAdmin
New-SPUser -UserAlias ($comp.Name + “\User2″) -Web $siteUrl -Group “Home Owners”
New-SPUser -UserAlias ($comp.Name + “\User3″) -Web $siteUrl -Group “Home Members”
New-SPUser -UserAlias ($comp.Name + “\User4″) -Web $siteUrl -Group “Home Visitors”
} catch [Exception] { Write-OutPut $_.Exception.ToString() | Out-File $logFile -append}
To Create a new WebApplication
try
{
$comp = Get-WmiObject -Class Win32_ComputerSystem
$hostHeader = “mywebsite.com”
$port = 80
$siteUrl = “http://” + $hostHeader
$appPoolName = “SharePoint – ” + $hostHeader + $port
$solPath = (Get-ChildItem (“..\bin\Debug\” + $solName)).FullName
$ownerAccount = [System.Security.Principal.WindowsIdentity]::GetCurrent().Name
$logFile = $workFolder + “\ReinstallPersonalDev.log”
$appName = “My Web Site”
$appPoolAccount = “domain\username “
$databaseName = “Mywebsite_Content_” + $comp.Name
$databaseServer = “MyDatabase”
} catch [Exception] { Write-OutPut $_.Exception.ToString() | Out-File $logFile -append}
Delete WebApplication
try {
Write-Output “Deleting web application” | Out-File $logFile -append
Remove-SPWebApplication -Identity $siteUrl -DeleteIISSite:$true -RemoveContentDatabases:$true -Confirm:$false | Out-File $logFile –append
Start-Sleep -Seconds $deploySleepSecs | Out-File $logFile -append
} catch [Exception] { Write-OutPut $_.Exception.ToString() | Out-File $logFile -append}
Recycling app pool
try {
Write-Output “Recycling app pool” | Out-File $logFile -append
$appPool = Get-WMIObject -NameSpace “root\MicrosoftIISv2″ -Class “IIsApplicationPool” | Where-Object {$_.Name -eq “W3SVC/APPPOOLS/$appPoolName”}
$appPool.Recycle() | Out-File $logFile -append
} catch [Exception] { Write-OutPut $_.Exception.ToString() | Out-File $logFile -append}
To create a new SiteCollection
try {
Write-Output “Creating site collection” | Out-File $logFile -append
New-SPSite -Url $siteUrl -OwnerAlias $ownerAccount -Name $rootTitle -Template $rootTempl -Confirm:$false
Start-Sleep -Seconds $deploySleepSecs | Out-File $logFile -append
} catch [Exception] { Write-OutPut $_.Exception.ToString() | Out-File $logFile -append}
To get the number of site collections:
(Get-SPSite).Count
To remove all site collections without confirmations:
Get-SPSite | Remove-SPSite -Confirm:$false
To Delete a SiteCollection
try {
Write-Output “Deleting site collection” | Out-File $logFile -append
Remove-SPSite -Identity $siteUrl -Confirm:$false | Out-File $logFile -append
} catch [Exception] { Write-OutPut $_.Exception.ToString() | Out-File $logFile -append}
To create new site:
try {
Write-Output “Creating first-level sites” | Out-File $logFile -append
New-SPWeb -Url ($siteUrl + “/” + $firstSiteUrl) -Name $ firstSiteTitle -Template $templName
Start-Sleep -Seconds $deploySleepSecs | Out-File $logFile -append
}catch [Exception] { Write-OutPut $_.Exception.ToString() | Out-File $logFile -append}
To create new task list in all sites:
Get-SPWeb | ForEach {$_.Lists.Add(“My Tasks”, “”,$_.ListTemplates["Tasks"])}
To create new task list in site:
Get-SPWeb $url | ForEach {$_.Lists.Add(“My Tasks”, “”,$_.ListTemplates["Tasks"])}
To enumerate available workflows:
Get-SPWeb $url | Select -Expand WorkflowTemplates | Select Name
To enumerate all document libraries in your site:
Get-SPWeb $url | Select -Expand Lists | Where {$_.BaseType -eq “DocumentLibrary”}
Adding solutions
$solPath = (Get-ChildItem (“..\bin\Debug\” + $solName)).FullName
try {
Write-Output “Adding solutions” | Out-File $logFile -append
Add-SPSolution -LiteralPath $solPath -Confirm:$false | Out-File $logFile -append
Start-Sleep -Seconds $deploySleepSecs | Out-File $logFile -append
} catch [Exception] { Write-OutPut $_.Exception.ToString() | Out-File $logFile -append}
Installing solutions
try {
Write-Output “Installing solutions” | Out-File $logFile -append
####TODO: use line below after we have webapp-scoped items in solution, now must be deployed globally
#Install-SPSolution -Identity $solName -WebApplication $siteUrl -GACDeployment -Confirm:$false -force
Install-SPSolution -Identity $solName -GACDeployment -Confirm:$false -force | Out-File $logFile -append
Start-Sleep -Seconds $deploySleepSecs | Out-File $logFile -append
} catch [Exception] { Write-OutPut $_.Exception.ToString() | Out-File $logFile -append}
Uninstalling solutions
try {
Write-Output “Uninstalling solutions” | Out-File $logFile -append
####TODO: use line below after we have webapp-scoped items in solution, now must be deployed globally
#Uninstall-SPSolution -Identity $solName -WebApplication $siteUrl -Confirm:$false | Out-File $logFile -append
Uninstall-SPSolution -Identity $solName -Confirm:$false | Out-File $logFile -append
Start-Sleep -Seconds $deploySleepSecs | Out-File $logFile -append
} catch [Exception] { Write-OutPut $_.Exception.ToString() | Out-File $logFile -append}
Removing solutions
try {
Write-Output “Removing solutions” | Out-File $logFile -append
Remove-SPSolution -Identity $solName -Confirm:$false | Out-File $logFile -append
Start-Sleep -Seconds $deploySleepSecs | Out-File $logFile -append
} catch [Exception] { Write-OutPut $_.Exception.ToString() | Out-File $logFile -append}
Activating Feature
try {
Write-Output “Activating feature” | Out-File $logFile -append
Enable-SPFeature -Identity featureName -Url $siteUrl -Confirm:$false
} catch [Exception] { Write-OutPut $_.Exception.ToString() | Out-File $logFile -append}
Working with Content
Show all items in a site:
Get-SPWeb $url | Select -Expand Lists | Select -Expand Items | select
Name, Url
Show only documents:
Get-SPWeb $url | Select -Expand Lists | Where {$_.BaseType -eq
“DocumentLibrary”} | Select -Expand Items | select Name, Url
Search for item:
Get-SPWeb $url | Select -Expand Lists | Select -Expand Items | Where
{$_.Name -like “*.doc”} | select Name, Url
To create a new document in a document library:
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()
Get-SPWeb $WebUrl | ForEach {$_.Lists[$ListName]} | ForEach
{$_.RootFolder.Files.Add($DocumentName, $stream,
$true);$_.Update()}
}
New-SPFile -WebUrl “http://mycompany/sites/mysite” -ListName
“Shared Documents” -DocumentName “MyFirstDocument” -Content
“Power Blues”
Recycle Bin
To find an item by its name in the Recycle Bin for a site:
(Get-SPWeb “http://sp2010dc” ).RecycleBin | Where {$_.Title -match “cool”}
Then use the item ID to restore it:
(Get-SPWeb “http://sp2010dc” ).RecycleBin.Restore( -”b23d2d41-cd6a-4471-a89 1 -c86f83563e11″ )
For Site Collection Recycle Bin use:
(Get-SPSite).RecycleBin
No comments:
Post a Comment