PowerGUI is a great tool to write scripts. I just play around the PowerShell script and add here for furture reference:
The first step in connecting to SharePoint is to load the necessary .Net library. This can be done with the following command:
[system.reflection.assembly]::LoadWithPartialName("Microsoft.Sharepoint")
$site = New-Object Microsoft.SharePoint.SPSite("http://adbmoss")
# Connect to the site collection http://adbmoss and store the object in the $site variable
$site | get-member
# Display all the properties and methods for the object in $site
$site.set_readonly($True)
# Set the site collection to read only. No updates are allowed
$site.set_readonly($False)
# Set the site collection back to read-write
$root = $site.rootweb
# Connect to the root site in the site collection and store the object in $root
$root | get-member
# Display all the properties and methods for the object in $root
$root.lists | format-table -property title, author
# Display title and authors for all the lists in the root site
$root.lists | ?{$_.iscatalog -eq $null} | format-table -property title, author
# Filter the lists to show only the user lists. The $_ symbol refers to the object in the piple line. The ? symbol is an alias for the where-object commandlet.
$root.listtemplates | format-table -property name
# Show all the available list templates for the root web
$TempCal=$root.listtemplates["Calendar"]
# Store the Calendar template in a variable $TempCal
$root.lists.add("Events","Company Events", $tempCal)
# Create a list called “Events” with Description “Company Events” on the calendar template
import-csv dept.csv | %{$root.lists.add($_.Dept, $_.Descr,$root.listtemplates[$_.Temp])}
# Create a new list for ever line in a CSV file where the name for the list is saved in the Dept column of the file, the description is in the Descr column, and the type of list to create is in the Temp column. First check that the lists are correctly created before you continue. The % symbol is an alias for the foreach-object commandlet and places each instance of the collection in the pipeline one after the other.
import-csv dept.csv | %{$root.lists[$_.Dept].delete()}
# Use the CSV file again to delete all the create lists again.
You can user PowerShell to provision additional columns in a custom list as well. The following commands show how to accomplish that.
[system.reflection.assembly]::LoadWithPartialName("Microsoft.Sharepoint")
# Load SharePoint library
$site = New-Object Microsoft.SharePoint.SPSite("http://adbmoss")
# Connect to the site collection http://adbmoss and store the object in the $site variable
$root = $site.rootweb
# Connect to the root site in the site collection and store the object in $root
$root.lists.add("CTest","Custom Test",$root.listtemplates["Custom List"])
# Create a new list called “CTest” from the template “Custom List”
$list = $root.lists["CTest"]
# Set a variable $list to the custom list that was just created.
$list.fields.add("T1","Text",0)
# Create a new field of type Single Line Text with the name of “T1”. The field is not required (last parameter)
The scenario below was that the documents were already in the library and they had a spreadsheet with the id number of the document and some additional meta-information. This can also be done in PowerShell. The first step is to save the spreadsheet in CSV format or any other format that PowerShell can easily import. The following code shows how to go about importing the metadata.
[system.reflection.assembly]::LoadWithPartialName("Microsoft.Sharepoint")
# Load SharePoint library
$site = New-Object Microsoft.SharePoint.SPSite("http://adbmoss")
# Connect to the site collection http://adbmoss and store the object in the $site variable
$root = $site.rootweb
# Connect to the root site in the site collection and store the object in $root
$docs = $root.lists["Shared Documents"]
# Store the Shared Documents document library in a variable $Docs
import-csv DocProps.CSV | %{$item = $Docs.items[$_.id]; $item["Title"] = $_.Title; $Item["Group"] = $_.group; $Item.Update()}
# Loop through the csv file and update the properties
The last line of the code is quite complex, so what it means are:
import-csv DocProps.CSV # Import the data from the DocProps CSV file
| # Pipe the results to the group
%{ # For each (%) line in the csv file do begin ({)
$item = $Docs.items[$_.id]; # set the variable $item to the entry in the list where the id corresponds to the id column in the file. The semicolon (;) indicates the separate command is to follow
$item["Title"] = $_.Title; # set the title of $item to Title in the file
$Item["Group"] = $_.group; # set the Group metadata property to the group Column in the file
$Item.Update() # update the entry $item in the list
} # End (}) the loop