Powershell and Sharepoint library

…this post was published on 2012 October 20.

as usual, starting from a question, let’s work togeter and see what Powershell can do to help us.

First think to do, open Sharepoint 2010 Management Shell Console, using an admin account.
Next let’s suppose that we have a web application, with the URL: http://domain/
to get  the object associated using Powershell, we use next command:

   > $web=get-spweb -identity “http://domain”

Wow. But what about this object. To get more infos:

   > $web | get-member

and you will receive a lot of lines …. Methods or Properties. One of them, is named ” Lists”.

If you tape:

   >foreach ( $each_list in $web.Lists ) {$each_list.title }

you will get all lists’ title in that web app.

But we need a specific list, named “MY_LIST_NAME“.
To filter and focus to that list only use this command:

   >$my_list = $web.lists | Where-Object { $_.title -eq “MY_LIST_NAME” }

To get all documents from this library, use:
       foreach ($document in $my_list.items )
To display the results use
 {Title”+”`t”+$document.name ,”`nDocument_Size`t”+$document.file).length  }
so the command is:
   > ​foreach ($document in $my_list.items ) {“Title”+”`t”+$document.name ,”`nDocument Size `t”+$document.file).length  }

What if you need to get all documents from all libraries from the web app?

Easy to change it
   > foreach ( $my_list in $web.lists ) { foreach ($document in $my_list.Items ) {$my_list.title+”-“+$document.name } }

This entry was posted in Powershell, Sharepoint, Uncategorized. Bookmark the permalink.