Extract a wsp using Powershell (SharePoint 2007)

I just found a cool way to extract a wsp that has already been deployed using powershell:

[System.Reflection.Assembly]::LoadWithPartialName(“Microsoft.SharePoint”)
$site = new-object Microsoft.SharePoint.SPSite(“<site collection url>”)
$farm = $site.webapplication.farm
$file = $farm.solutions.item(“siterequest.wsp”).solutionfile
$file.SaveAs(“<path>\siterequest.wsp”)

 

Advertisements

Powershell: SharePoint 2007 vs 2010

It still amazes me the differences between writing powershell for SharePoint 2007 and SharePoint 2010. For example, look at the differences when you try to find the number of spwebs within a farm that have a template “STS#1”:

2010:

Add-PSSnapin “Microsoft.SharePoint.Powershell”

(Get-SPWebApplication -IncludeCentralAdministration | Get-SPSite -Limit all| Get-SPWeb -Limit all | where{$_.WebTemplate -eq “STS” -and $_.WebTemplateId -eq “1”}).count

2007:

[System.Reflection.Assembly]::LoadWithPartialName(”Microsoft.SharePoint”)

$total = 0

$farm = [Microsoft.SharePoint.Administration.SPFarm]::Local

foreach($spService in $farm.Services) {

if($spService -is [Microsoft.SharePoint.Administration.SPWebService]) {

foreach($webapp in $spService.WebApplications) {

foreach($site in $webapp.Sites) {

foreach($web in $site.AllWebs) {

if($web.WebTemplate -eq “STS” -and $web.WebTemplateId -eq “1”) {

$total +=1

}

}

}

}

}

}

Write-host $total

Create a SharePoint 2010 Content Type using Powershell

Creating a content type using powershell:

$site = get-spsite $url

$web = $site.openweb()

$ctypeName = “myName”

$ctypeParent = $web.availablecontenttypes[“Document”]

$ctype = new-object Microsoft.SharePoint.SPContentType($ctypeParent, $web.contenttypes, $ctypeName)

$web.contenttypes.add($ctype)

$web.fields.add(“myField”, ([Type]”Microsoft.SharePoint.SPFieldType”)::Text, $false)

$field = $web.fields.getfield(“myField”)

$fieldLink = new-object Microsoft.SharePoint.SPFieldLink($field)

$ctype.fieldlinks.add($fieldLink)

$ctype.Update()

$web.Dispose()

$site.Dispose()