Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

Edit This is for SharePoint 2007

I have this powershell script:

## Reference to SharePoint DLL 
[System.Reflection.Assembly]::Load(“Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c”) 
[System.Reflection.Assembly]::Load(“Microsoft.SharePoint.Portal, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c”)
## Probable location of sharepoint STSADM utility program 
$stsadm = "$env:programfiles\Common Files\Microsoft Shared\Web Server Extensions\12\BIN\STSADM.EXE" 

############################################ 
# Activate-feature-onSite [-feature  |-url  ] 
############################################ 
$spsite=[Microsoft.SharePoint.SPSite]("site-url"); 

foreach($web in $spsite.AllWebs)
{
  $websiteurl= $web.url
  $sResult = &stsadm -o deactivatefeature -id '6C45F6A6-17CC-49f4-A30F-4424C7DF3563' -url $websiteurl -force
  if(!($sResult -like "*Operation completed successfully*")){ 
  Write-Host -ForegroundColor "red" -BackgroundColor "white" "Deactivation of feature ‘’ for ‘$websiteurl’ Failed!" 
 } 
 else{
  Write-Host -ForegroundColor "blue" -BackgroundColor "white" "Deactivation of feature 6C45F6A6-17CC-49f4-A30F-4424C7DF3563 for ‘$websiteurl’ Worked! `n $sResult" 
 }
} 
$spsite.Dispose()

And before it is deployed id like to run it by some pros and see if I am correctly disposing of the site, and if i should dispose of the webs aswell.

Ive never used powershell before, so im not sure if it follows the regular disposal practices, or if it does the disposal itself, etc.

Any feedback would be welcome!

share|improve this question
add comment

2 Answers

up vote 2 down vote accepted

Take a look at this answer:

How bad is it to not dispose() in Powershell?

When you close the PowerShell.exe process, the memory is freed. If you need to dispose objects to keep memory pressure down (important in production environments or if you're looping over all sites/webs), make sure to dispose. If not, you don't need to worry about disposing.

In your case $spsite.Dispose() doesn't do anything because it is last command in your script so memory will be freed when script ends.

share|improve this answer
    
What about the SPWeb objects? This script is going to be run multiple times. –  John S Feb 7 '12 at 20:30
1  
It does matter only if number of webs on your site is huge, really huge. So by using $web.Dispose() in your foreach loop you can save some memory. It doesn't cost you anything to put that extra line :) –  Vedran Rasol Feb 7 '12 at 21:05
    
Yea, there are a couple hundred webs in the biggest site collection so it will definitely help. –  John S Feb 9 '12 at 17:29
add comment

Consider using the Start-SPAssignment/Stop-SPAssignment pair. Here is a post that describes using it - SharePoint 2010, PowerShell and Disposing.

share|improve this answer
    
there's a little typo error in the above link - in the Simple Assignment example: 'Start-SPAssignment -GlobalGlobal' should read 'Start-SPAssignment -Global' –  Supriyo SB Chatterjee Feb 7 '12 at 19:51
    
Forgot to mention this is for sharepoint 2007. –  John S Feb 7 '12 at 20:31
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.