none
How to Run a PowerShell Script?

    Question

  • Pardon my amatuer question! How do I run a powershell script? Could someone walk me through the steps to run this script:

    if((Get-PSSnapin | Where {$_.Name -eq "Microsoft.SharePoint.PowerShell"}) -eq $null) {
    Add-PSSnapin Microsoft.SharePoint.PowerShell;
    }

    Get-SPSite -Identity http://sitecollectionurl | Get-SPWeb -Limit ALL | ForEach-Object {

    Disable-SPFeature –Identity DocumentRouting –url $_.Url –Confirm:$false

    $dropOffLibrary = $_.Lists["Drop Off Library"]

    $dropOffLibrary.AllowDeletion = "True"
    $dropOffLibrary.Update()
    $dropOffLibrary.Delete()

    }

    Saturday, March 31, 2012 10:01 PM

Answers

  • Hi.

    With pleasure.

    1, in for example c:\scripts\ create a textfile, name it myscript.ps1 (all on the SP server)
    2, make sure you have permissions to run the script. Often, you need permissions to the content databse directly(by running the add-spshelladmin command)
    3, start a Powershell prompt and run it 'as administrator'.
    4, Set the execution policy: Set-Executionpolicy Remotesigned
    4, In the prompt, find the path: CD C:\scripts
    5, type myscript.ps1 and hit enter.

    This will run the script, what happens is (after loading the pssnapin for SHarePoint if needed) that this script will get all subsites under the site collection sitecollectionurl, on the webs it will try to disable the feature 'DocumentRouting' and then on the List called 'Drop Off Library' set deletion to true.
    What the last line does, is that it deletes the Document Library ($dropOffLibrary.Delete()).

    So in effect, the script will delete every list with the name 'Drop Off Library'  in the SIte Collection. It will probably also generate a few errors in red where it can't find that library/list, but that can be disregarded.

    You could also do it like this:

    if((Get-PSSnapin | Where {$_.Name -eq "Microsoft.SharePoint.PowerShell"}) -eq $null) {
     Add-PSSnapin Microsoft.SharePoint.PowerShell;
    }
    $SPSite = Get-SPSite -Identity http://blksthl-sp1/sites/enfobranding
    ForEach ($SPWeb in $SPSite.allwebs) {
     
    write-host $SPWeb.Url
     
    $dropOffLibrary = $SPWeb.Lists["Drop Off Library"]
     
    $dropOffLibrary.AllowDeletion = "True"
    $dropOffLibrary.Update()
    $dropOffLibrary.Delete()
     
    }

    Hope this helps.

    Regards


    Thomas Balkeståhl - Technical Specialist - SharePoint - http://blog.blksthl.com
    Download the SharePoint Branding Project here

    • Marked as answer by EdwardLee Saturday, March 31, 2012 10:54 PM
    Saturday, March 31, 2012 10:39 PM

All replies

  • Hi.

    With pleasure.

    1, in for example c:\scripts\ create a textfile, name it myscript.ps1 (all on the SP server)
    2, make sure you have permissions to run the script. Often, you need permissions to the content databse directly(by running the add-spshelladmin command)
    3, start a Powershell prompt and run it 'as administrator'.
    4, Set the execution policy: Set-Executionpolicy Remotesigned
    4, In the prompt, find the path: CD C:\scripts
    5, type myscript.ps1 and hit enter.

    This will run the script, what happens is (after loading the pssnapin for SHarePoint if needed) that this script will get all subsites under the site collection sitecollectionurl, on the webs it will try to disable the feature 'DocumentRouting' and then on the List called 'Drop Off Library' set deletion to true.
    What the last line does, is that it deletes the Document Library ($dropOffLibrary.Delete()).

    So in effect, the script will delete every list with the name 'Drop Off Library'  in the SIte Collection. It will probably also generate a few errors in red where it can't find that library/list, but that can be disregarded.

    You could also do it like this:

    if((Get-PSSnapin | Where {$_.Name -eq "Microsoft.SharePoint.PowerShell"}) -eq $null) {
     Add-PSSnapin Microsoft.SharePoint.PowerShell;
    }
    $SPSite = Get-SPSite -Identity http://blksthl-sp1/sites/enfobranding
    ForEach ($SPWeb in $SPSite.allwebs) {
     
    write-host $SPWeb.Url
     
    $dropOffLibrary = $SPWeb.Lists["Drop Off Library"]
     
    $dropOffLibrary.AllowDeletion = "True"
    $dropOffLibrary.Update()
    $dropOffLibrary.Delete()
     
    }

    Hope this helps.

    Regards


    Thomas Balkeståhl - Technical Specialist - SharePoint - http://blog.blksthl.com
    Download the SharePoint Branding Project here

    • Marked as answer by EdwardLee Saturday, March 31, 2012 10:54 PM
    Saturday, March 31, 2012 10:39 PM
  • Thank you. Appears to have worked like a charm!
    Saturday, March 31, 2012 10:54 PM
  • You're welcome.

    A tip for new PowerShell users, try out the Get-Help commend for every command, for example:

    Get-Help Get-SPSite

    or with examples:

    Get-Help Get-SPSite -Examples
    

    And, use the Tab key to fill in commands, flags or paths...ex:

    Get-SPS [Tab] -> Get-SPSite

    Enjoy!


    Thomas Balkeståhl - Technical Specialist - SharePoint - http://blog.blksthl.com
    Download the SharePoint Branding Project here

    Saturday, March 31, 2012 11:00 PM