Create Document Libraries and Folders in SharePoint using PowerShell

If you have not used PowerShell let me show you a nifty little script that saved hours of repetitive clicking through usability challenged tools (aka SharePoint Designer):

[System.Reflection.Assembly]::Load("Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c");
$site = new-object Microsoft.SharePoint.SPSite("http://myweb");
$siteweb = $site.OpenWeb();
$webs = $siteweb.Webs;
foreach($web in $webs)
{
    Write-Host $web.Title;
    $listtemplate = $web.ListTemplates["Document Library"];
    $listId = $web.Lists.Add("ShortName", "", $listtemplate);
    $list = $web.Lists.GetList($listId, $true);
    $list.Title = "Long Descriptive Title";
    $list.OnQuickLaunch = $True;
    $folder = $list.Folders.Add("", [Microsoft.SharePoint.SPFileSystemObjectType]::Folder, "SubFolder1");
    $folder.Update();
    $list.Update();
}

This script will cycle through all of the sub-webs in a SharePoint site, add a new document library, and sub-folder to that library.  While it does not sound like a difficult task, going through either the web-based interface or the SharePoint Designer interface is quite mundane. There are a bunch of unnecessary clicks that, thanks to a UI that has no consideration for repetitive or advanced usage, make the process painful at best.

It is a shame there is not more emphasis on managing SharePoint through PowerShell.  There is a lot of opportunity to build cmdlets that would have made the above all so much cleaner.  The Exchange Server team managed to do this with success. Speaking as a user, developer, and IT Professional, I would rather have seen something like Excel Services ship as an out-of-band release in favour of having better migration, management tools, and complete documentation. For some more tips and tricks check out Colin Bryne's blog for a few other samples.

Comments Subscribe to Post Comments Feed

George Kesler said:

I'm looking for a way to update some of the items in my inventory list on WSS3 from Powershell. For example, run a WMI query to determine the IP address of a server and place the result to the IP field of the list:

Server Name: IP Address    : CPU Speed:

TestServ   : 192.168.0.200 : 6GHz

I got some basic ideas from

www.u2u.info/.../Post.aspx

but can't really put the whole thing together. is it even possible to do?

Have Your Say