Home > Powershell > Powershell: Importing Items into a Sharepoint List

Powershell: Importing Items into a Sharepoint List

I was teaching a Windows Server 2003 to 2008 upgrade class onsite recently for a client and it included several Powershell examples on performing such tasks as installing a role\feature or managing AD users\groups.   While on break I got to speaking with one of the tech’s and asked if he was currently utilizing Powershell to automate common day to day tasks.  His response was not what I expected as he made the statement that he couldn’t see any use for Powershell.   My initial reaction was suprise as I thought any decent tech would want to embrace such a robust and versitile automation technology, especially one that provides a standard platform for managing all things Microsoft.  

I decided to probe a bit further and see if I could get him started with scripting and asked that he think about a task that he needed to do everyday\everyweek that could benefit from some sort of automation.  His first example was to view updated information on the servers he supported as the current method of manually updating\retrieving this info from an excel spreadsheet was never consistent and in the end he almost always had to RDP  and view the information first hand.   I thought to myself that this is a perfect opportunity to demonstrate, not only Powershell but Sharpeoint 2010 as well.

Here is the demo script I came up with that will query an array of computers and build a custom object with only those desired properties and then import each object  into a custom Sharepoint list called “Inventory”.   It’s a great way to create an inventory of servers\desktops and then use Sharepoint to add custom columns, sorting, filtering and even workflows to make the list truly dynamic.

It’s composed of two functions: Get-Inventory and Import-ListItems.

Function Get-Inventory {             

   [array]$computers = "$env:computername",
   $global:computer_array = @()

   foreach($i in $computers) {
     $CS = gwmi win32_computersystem -ComputerName $i |
        select name,model
     $OS = gwmi win32_operatingsystem -ComputerName $i |
        select osarchitecture,serialnumber            

     $obj = New-Object PSObject -Property @{
     Architecture = $OS.osArchitecture
     SerialNumber = $OS.SerialNumber

   $global:computer_array += $obj
  Write-Output $computer_array
}#End Function            

Function Import-ListItems {             

   $site = "http://intranet",
   $spweb = (Get-SPWeb -Identity $site),
   $List = $spweb.lists["Inventory"]

   Foreach($row in $computer_array) {
     $item = $List.items.add()
     $item["Name"] = $row.Name
     $item["Model"] = $row.Model
     $item["Architecture"]= $row.Architecture
     $item["Serial Number"] = $row.SerialNumber
 }#End Function            

Get-Inventory -computers $env:computername             

Import-ListItems -site "http://intranet"
  1. Yasaf
    April 18, 2012 at 9:32 am

    Thanks for that script.
    How can I specify OU (or OUs) in case I want to limit the results to specific objects?

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: