Posts Tagged Powershell; codesamples

SharePoint Powershell codesamples

Here are some powershell codesamples that I use frequently and got tired of finding the code everytime:

Task: Looping All sites and webs in a webapplication:

Add-PSSnapin Microsoft.SharePoint.PowerShell -erroraction SilentlyContinue
$htmlPrefix = "http://" # or use https:// for SSL sites
#$appHostNames = @("", "", "")
foreach($appHostName in $appHostNames)
  $siteUrl = $htmlPrefix + $appHostName
  Write-Host "Do something with all sites under this webapp: " $siteUrl
  $rootSite = New-Object Microsoft.SharePoint.SPSite($siteUrl)
  $spWebApp = $rootSite.WebApplication
  foreach($site in $spWebApp.Sites)
    Write-Host "You can now do something with this SPSite object : " $site.RootWeb.Url
    foreach($web in $site.AllWebs)
      Write-Host "You can now do something with this SPWeb object : " $web.Title "("$web.Url")"
      # Or if you need to access Publishing web object
      $pWeb = [Microsoft.SharePoint.Publishing.PublishingWeb]::GetPublishingWeb($web)

Task: Setting New page default pagelayout
This example set the Publishings webs default pagelayout to “MyCustomPageLayouts.aspx” which is located in the gallary. The way to do this by the UI is Site Settings > Page layouts and site templates (Look and Feel section) > New Page Default Settings (section)  (/_Layouts/AreaTemplateSettings.aspx)

Add-PSSnapin Microsoft.SharePoint.PowerShell -erroraction SilentlyContinue
$site = Get-SPSite ""
$web = $site.RootWeb
$pweb = [Microsoft.SharePoint.Publishing.PublishingWeb]::GetPublishingWeb($web)
$layout = $pweb.GetAvailablePageLayouts() | ? {$_.Name -eq "MyCustomPageLayout.aspx"}
$pweb.SetDefaultPageLayout($layout, $false)

Task: Adding specific Mime Type to Specific Webapplication
This example (found at Mathieus blog) shows how you can add a specific MimeType to the Allowed Mimetypes for a WebApplication. This example adds the PDF mime type. A large list of Mime types can be found here

Add-PSSnapin Microsoft.SharePoint.PowerShell –ErrorAction SilentlyContinue
#Mime Type Examples: "application/pdf, text/html, text/xml, application/x-shockwave-flash"
$mimetype = "application/pdf"
$webapp = Get-SPWebApplication ""
If ($webapp.AllowedInlineDownloadedMimeTypes -notcontains $mimetype)
  Write-Host "Adding MIME Type..."    
  Write-Host "Done."
  Write-Host -ForegroundColor Green "MIME type is already added."

By this way you can add specific allowed mimetypes without having to switch Browser File Handling from Strict to Permissive, which is normally done in Central administration > Manage Webapplications > Mark a Webapplication and select ‘General Settings’ in the Ribbon. With this option set to Permissive, all Mimetypes are allowed.

Browser File Handling settings

Task: Update list item
This example show how to update a listitem. In this case a DateTime field.

$SPSite = new-object Microsoft.SharePoint.SPSite("http://server/sites/sc")
$SPWeb = $SPSite.RootWeb
$SPList = $SPWeb.Lists["YourList"]
$SPItem = $SPList.GetItemById("4")
$SPItem["Some date"] = "01/01/2011 00:00:00"

Task: Get a list of all WebTemplates with information

This example show how to get a complete list of all web templates that can be used to create sites, including templates saved by users to the site collections template gallary.

Get-SPWeb http://YourSiteUrl | %{$_.GetAvailableWebTemplates($_.Language)} | Format-List

This post will be updated as time goes by and I run into SharePoint Powershell issues…


1 Comment