More PowerShell Scripting for MOSS

Here are some examples of working with MOSS lists via PowerShell.

All of these assume that Get-SPWeb() is defined. You can get that from http://sharepoint.microsoft.com/blogs/zach/Lists/Posts/Post.aspx?List=90bbfd11%2Dc9a5%2D45cf%2Da77e%2D19559aae81ae&ID=7.

  1. Adding a Calendar list to an SPWeb

    function global:Add-Calendar($url,$title){

        $destWeb = get-SPweb -url $url

        $caltemplate = $destWeb.ListTemplates[“Calendar”]    

        $listtitle = $title+” Events”

        $destWeb.Lists.Add($listtitle,”Departmental calendar of events.”,$caltemplate)

        Dispose-SPWeb($destWeb)

    }

  1. Adding an Announcements list to an SPWeb

    function global:Add-News($url,$title){

    $destWeb = get-SPweb -url $url

    $template = $destWeb.ListTemplates[“Announcements”]

    $destWeb.Lists.Add($title+” News”,”News and announcements.”,$template)

    Dispose-SPWeb($destWeb)

    }

  2. Adding a Links list to an SPWeb

    function global:Add-LinksList($url){

        $destWeb = get-SPweb($url)

        $template = $destWeb.ListTemplates[“Links”]

        $destWeb.Lists.Add(“Related Sites”,”Links to relevant other sites.”,$template)

        Dispose-SPWeb($destWeb)

    }

  3. Adding all three

    function global:Add-NewsCalAndLinks($url,$title){

        Add-News -url $url -title $title

        Add-Calendar -url $url -title $title

        Add-LinksList($url)    

    }

  4. Printing out a list of the Lists

    $web = Get-SPWeb($url)

    $web.Lists | ForEach-Object -process {$_.Title}

    Dispose-SPWeb($web)

  5. Printing out a list of the ListTemplates

    $web = Get-SPWeb($url)

    $web.ListTemplates | ForEach-Object -process {$_.Name}

    Dispose-SPWeb($web)

–Michael