MOSS 2007 Script Collection

I’ve been working with SharePoint and PowerShell for quite some time now and I would like to share a couple of Scripts that I use when scripting MOSS. Actually, it’s 44 Scripts, Loads of Coffee and alot of long nights :) so I hope you enjoy them. There are Links at the bottom of the Post where you can Download all the scripts, or you can Download all scripts from the link below.

moss-script-collection.zip

Down to business then. I’ll put up a scenario based on the Session that I used at the SharePoint UserGroup meeting in Stockholm Sweden, explaining all Steps and how to use the Scripts.

First I set up a Folder where I keep all my scripts, I use C:\Scripts. Then I add the folder to the Windows Path.


PS > $env:path = $env:path + ";c:\scripts"

 

Adding the Script folder to the Environment Path let’s Us Call the script simply by typing it’s name. so instead of typing:


PS > .\Get-SPSite.ps1 -url http://moss

 

We can Type:


PS > Get-SPSite -url http://moss

 

Note, Don’t forget to set the Execution-Policy, otherwise, the scripts won’t run. Read more about it here


PS > Set-ExecutionPolicy RemoteSigned

 

All Scripts include a help text that explains how to use the Script. To access the helptext simply type the Script Name followed by -help.


PS > Get-SPSite -help

 

Now We can start Scripting MOSS!

Loading SharePoint Assemblies

 

The first thing we have to do in order to Access the MOSS 2007 Assemblies is Loading them into PowerShell. Even though they Exist in our Windows Environment, PowerShell isn’t aware of them. By Loading them into our Global Assembly Cache, PowerShell can access and use the MOSS 2007 .NET Classes.


[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")

 

Get-SPSite.ps1

 

First, We’ll Check out the Site Collection. Here’s how we can add our Site Collection to a variable using the Get-SPSite.ps1 script and retrieve information from it. When working with SPSite or OpenWeb objects in the PowerShell console you have to dispose of the object on the same row as the command, otherwise they will leak memory. This only applies when you create SharePoint objects directly in the PowerShell console. When run in functions and scripts, the commands are run in a single thread. for more information click here


PS > $SPSite = Get-SPSite -url http://moss; $SPSite | Select Url, Port, Owner | Format-List; $SPSite.Dispose()

Url   : http://moss
Port  : 80
Owner : POWERSHELL\administrator

 
We can also get usage Information:


PS >  $SPSite = Get-SPSite -url http://moss; $SPSite.Usage; $SPSite.Dispose()


Storage           : 550113
Bandwidth         : 0
Visits            : 0
Hits              : 0
DiscussionStorage : 0

 

And even information from the WebApplication Pool 


PS > $SPSite = Get-SPSite -url http://moss; $SPSite.WebApplication.ApplicationPool | Select DisplayName, Status | Format-List; $SPSite.Dispose()


DisplayName : SharePoint - 80
Status      : Online

 

Try Pipe:ing the $SPSite object to Get-Mameber and check out all Available methods and properties available. 


PS > $SPSite = Get-SPSite -url http://moss; $SPSite | Get-Member; $SPSite.Dispose()

 

Get-SPWeb.ps1

 

The Get-SPWeb.ps1 Returns a Site instead of a Site Collection through the OpenWeb() method. Through it, we can modify the Title and description of a site, we can add Items, documents and lots more. Here’s an example on how you can change the Title and Descrition of a Site. 


PS > $OpenWeb = Get-SPWeb http://moss; $OpenWeb.Title; $OpenWeb.Dispose()

Home

PS > $OpenWeb = Get-SPWeb http://moss; $OpenWeb.Description; $OpenWeb.Dispose()

Home

PS >  $OpenWeb = Get-SPWeb http://moss; $OpenWeb.Title = "PowerShell Home"; $OpenWeb.Description = "Demo From PowerShell.nu"; $OpenWeb.Update(); $OpenWeb.Dispose()

 

Here’s What happened to my MOSS RootWeb Site after running these simple commands. 

moss01 

Get-SPList.ps1

 

We’ve accessed The Site Collection and a MOSS Site, Let’s look at how to access a List within a Site. We can do this through the Get-SPList.ps1 Script. We can use it to Get or Set List Information and even Get or Set Items and Fields in the List. 


PS > $SPList = Get-SPList -url http://moss -List Announcements; $SPList.ItemCount; $SPList.Dispose()

3

PS > $SPList = Get-SPList -url http://moss -List Announcements; $SPList.Items | ForEach { $_["Title"] }; $SPList.Dispose()

Get Started with Windows SharePoint Services!
Announcement Added through PowerShell
Demo from PowerShell.nu

 

Get-SPField.ps1

 

A List Contains Field which tell us what kind of information we can add to our Items. When Accessing a Field you can use the Get-SPField.ps1 script. The Field holds alot of information such as Type. 


PS > $Field = Get-SPField -url http://moss -List Announcements -Field Expires
PS > $Field.Type

DateTime

 

You can even edit information in the Field. Here’s an Example on How to Hide the Field in Edit Form. When a User Tries to Edit an item in the Announcements List, he won’t see the Expires Field. 


PS > $Field.ShowInEditForm = $False
PS > $Field.Update()

 

Get-SPView.ps1

 

A List Contains a Variaty of Views. Here’s how you Access a List View in MOSS 2007 using the Get-SPView.ps1 Script. 


PS > $View = Get-SPView -url http://moss -List "Shared Documents" -View "All Documents"

 

The View contains alot of Methods and Properties. Let’s take a look at the Clone() Method. 


PS > [void]$View.Clone("My Cloned View",100,$True,$False)

 

With this Simple Command, I’ve Created a new View Called “My Cloned View”. It’s basically a Copy of the “All Documents” View. 

Get-SPItem.ps1

 

When Accessing Items within a List, you can use the Get-SPItem.ps1 Script. Storing the Item in a Variable Allows us to Get And Modify it’s properties. The script includes an -All Switch that retrieves All Items in a List instead of just one item. 


PS > $Item = Get-SPItem  -url http://moss -List Announcements -Item "Get Started with Windows SharePoint Services!"

 

Now That we have an Item stored in the $Item Variable, we can Modify it. 


PS > $Item["Body"]

Microsoft Windows SharePoint Services helps you to be more eff ective by connecting people, information, and documents. For information on getting started, see Help.
PS > $Item["Body"] = "New Text in Body" PS > $Item.Update()

 

Another Nice Trick is Looping through all Items. First let’s Store them in a variable: 


PS > $AllItems = Get-SPItem -url http://moss -List Announcements -All

Now Let’s Set the Body on All Announcements in the Announcements List.


PS > $AllItems | ForEach { $_["Body"] = "New Text"; $_.Update() }

 

Add-SPAnnouncement.ps1

 

Next, let’s add an Announcement to our RootWeb Site. Using the Add-SPAnnouncement.ps1 sript, we can add through a One-Liner in PowerShell. 


PS > Add-SPAnnouncement -url http://moss -List "Announcements" -Title "Demo from PowerShell.nu" -Body "<h1>PowerShell</h1><p />is Cool!" -Expires (Get-Date).AddHours(1)

 

moss02 

Let’s say that you have multiple sites in your Site Collection and you want to add an Announcement on each site. This can be solved with a Simple ForEach: 


PS > $Sites = "http://moss/HR","http:moss/IT","http://moss/Production","http://moss/Sales"
PS > $Sites | ForEach {
>> Add-SPAnnouncement -url $_ -List "Announcements" -Title "Demo från PowerShell.nu" -Body "

is Coolt!" -Expires (Get-Date).AddHours(1)

 

This Code adds an announcement on each site in the $Sites Array. 

Add-SPCalendar.ps1

 

The Add-SPCalendar.ps1 script adds new Calendar Entries to a MOSS Calendar List. 


PS > Add-SPCalendar -url http://moss -List "Calendar" -Title "SharePoint - PowerShell Demo" -Location "Stockholm" -Description "PowerShell Demo" -StartTime (Get-Date) -EndTime (Get-Date).AddHours(4)

 

The -StartTime and -EndTime Parameters accept an [DateTime] object, which means that we can use the Get-date CmdLet to Add Dates. 

moss03 

Add-SPLink.ps1

 

Here’s a Script that adds Links to a MOSS Links List. It sets the Link, the Description of the Link and the Notes Field. 


PS > Add-SPLink -url http://moss -List "Links" -Link "http://www.powershell.nu" -Description "PowerShell.nu - Blog" -Notes "PowerShell Blog"

 

Here’s what our Newly created Link Item looks like: 

moss04 

Set-SPImageWebPart.ps1

 

On a Default installed MOSS, The Root Web uses the “Team Site” Template, which has an Image WebPart on the Right Displaying a SharePoint Services Image. Let’ go ahead and change the image using the Set-SPImageWebPart.ps1 Script. 


PS > Set-SPImageWebPart -url http://moss -WebPart "Site Image" -Image "C:\Demo\Files\PowerShell.jpg"

 

As you can see in the Image below, Our RootWeb Team Site looks a little PowerShelled. 

moss05 

Set-SPTheme.ps1

Now that we’ve customized our Site with a New “Site Image”, let’s go ahead and change the Theme. We’ll use the Set-SPTheme.ps1 script to achieve this.


PS > Set-SPTheme -Url http://moss -Theme obsidian

 

And with a simple One-Liner, we’ve changed the Theme on our Site. Doing without the script takes about two lines of code, but i Added the script since I think it’s pretty cool. 

moss06 

Upload-SPDocument.ps1

 

This Script lets you Upload Documents to a Document Library in MOSS. Here’s how it Works: 


PS > Upload-SPDocument -url http://moss -Folder "Shared Documents" -Document "C:\Documents\Excel SpreadSheet.xlsx"

 

If we look in our “Shared Documents” List, We can see that the Document is uploaded there now. 

moss07 

But What it we want to Upload one hundred documents ? Well, since we are using PowerShell, we can do it with a One-Liner. 


PS > gci C:\Documents | ForEach { Upload-SPDocument -url http://moss -Folder "Shared Documents" -Document $_.FullName }

 

This really shows the Power of Microsoft PowerShell 

Add-SPFolder.ps1

 

Now That we’ve Uploaded a couple of Files, Let’s Add a New Folder to our “Shared Documents”. We can do this by using the Add-SPFolder.ps1 script. 


PS > Add-SPFolder -Url http://moss -List "Shared Documents" -Name "New Folder"

 

If we want to Upload Documents Directly to our Folder we can use the Upload-SPDocument.ps1 Script again, we just point it to our New Folder. 


PS > Upload-SPDocument -url http://moss -Folder "Shared Documents/New Folder" -Document "C:\Documents\Excel SpreadSheet.xlsx"

 

Add-SPTextField.ps1

 

Let’s add a couple of Fields to our “Shared Documents” List. First, we’ll add a Document Owner Field. In this example we’ll use a simple Text field, but you could obviously use a User field instead. We’ll get back to the User field a little later in this post. 


PS > Add-SPTextField -url http://moss -List "Shared Documents" -Name "Document Owner" -Description "Document Owner"

 

This Adds a TextField to our List named “Document Owner”, if you want the field to Require information, simply add -Required to the command above. 

moss08 

Add-SPChoiceField.ps1

 

Next, We’ll add a Choice Field to our “Shared Document”. This can be done by using the Add-SPChoiceField.ps1 Script. Again, if you want the Field to Require information, just add -Required to the command. 


PS > Add-SPChoiceField -url http://moss -List "Shared Documents" -Name "Document Type" -Description "Type of Document" -Choices $("Excel","Word","PowerPoint")

 

Note the -Choices Paramteter. It takes an Array of arguments and Sets each argument as a Choice in the Field. Typing $(“Argument1″,”Argument2″) is a quick way to create an array and passing it as an argument to the script. 

moss09 

Add-SPFieldToView.ps1

 

Now that we have two new Fields in our “Shared Documents”, let’s add them to the “All Documents” View by using the Add-SPFieldToView.ps1 Script. 


PS > Add-SPFieldToView -url http://moss -List "Shared Documents" -Field "Document Owner" -View "All Documents"
PS > Add-SPFieldToView -url http://moss -List "Shared Documents" -Field "Document Type" -View "All Documents"

 

If we check out our “Shared Documents” list now, we can see that the Fields are Added to the “All Documents” View. You can, of course, use this on any type of List in MOSS 2007. 

moss10 

Set-SPItem.ps1

 

Here’s a Really Useful script when working with Items. The Set-SPItem.ps1 Script can either Create a New Item or Modify an existing Item. At first, Let’s take a look on how to Modify existing Items. By Default, the -field param is set to Title, but by changing it to Name, the script will check for a match in the Name Field and if it finds a match, it will update the Item, if it doesn’t find a match it will create a New Item. Note that when Creating Items in a Document Library you have to use the Upload-SPDocument.ps1 script. 

Note that the -Values takes a HashTable Array of Values to Set on the Item. Let’s say a List has Two Fields. A Title field and a Description Field. If I want to Create a New Item Called “New Item” and with The Description “My New Item Description” I could Simply type: 


PS > Set-SPItem -url http://moss -List "My List" -Name "New Item" -Values @{"Description" = "My New Item Description"}

 

If i Want to Edit the Item that i Created i Could reuse the Same Command, since it First looks for an Item Where the Title is Set to “New Item” 

Let’s say i Don’t know the Title of the Item but i Do know It’s Description. By using the -Field paramter i Could get the Correct Item and Modify it. 


PS > Set-SPItem -url http://moss -List "My List" -Name "My New Item Description" -Field Description -Values @{"Title" = "New Title"; "Description" = "Changed Title and Changed Description of Item"}

 

Let’s Take a Look at the Documents that we Added earlier. Here’s an example on adding MetaData on Documents in a Document Library:


PS > Set-SPItem -url http://moss -List "Shared Documents" -Name "Excel SpreadSheet.xlsx" -Field "Name" -Values @{"Document Type" = "Excel"; "Document Owner" = "Niklas Goude"}
PS > Set-SPItem -url http://moss -List "Shared Documents" -Name "Word Document.docx" -Field "Name" -Values @{"Document Type" = "Word"; "Document Owner" = "Niklas Goude"}
PS > Set-SPItem -url http://moss -List "Shared Documents" -Name "PowerPoint Presentation.pptx" -Field "Name" -Values @{"Document Type" = "PowerPoint"; "Document Owner" = "Niklas Goude"}

 

With these Commands I added information in the “Document Type” Field and the “Document Owner” Field on my three Documents, as the image below show. 

moss11 

Let’s Use this Script to Modify the Announcement That we Added Earlier. We’ll Change the Body of the Announcement. 


PS > Set-SPItem -url http://moss -List Announcements -Name "Demo from PowerShell.nu" -Values @{"Body" = "<h2>Modified With Set-SPItem.ps1</h2>"}

 

Here’s what the Announcemet looks like now: 

moss12 

Add-SPList.ps1

 

Now Let’s create our own Custom List. We’ll use the Add-SPList.ps1 Script to do this. Specifying the Type to “Custom List” tells the script to create a “Custom List”. You can, of course create any type of list available in MOSS through this script. 


PS > Add-SPList -url http://moss -Name "My List" -Description "My Custom Demo List" -Type "Custom List"

 

This Creates the List, but it doesn’t Add the List to the QuickLaunch Bar. Let’s check out how to Add a List to the QuickLaunchBar. 

Add-SPListToQuickLaunch.ps1

 

This Script adds lists to the Quicklaunch Bar in MOSS 2007. The Command below shows how you can use the script. 


PS > Add-SPListToQuickLaunch -url http://moss -List "My List"

 

This command adds the List to the QuickLaunch Bar. 

moss13 

Set-SPQuickLaunchOrder.ps1

 

Let’s Change the QuickLaunch Order. We’ll use the Set-SPQuickLaunchOrder.ps1 Script that moves a List to the Top of the QuickLaunch. 


PS > Set-SPQuickLaunchOrder -url http://moss -List "My List"

 

If we look at our site now, We can see that our QuickLaunch Order has been Changed. The “My List” List is now on top. 

moss14 

Now That we have a Custom List, Let’s Add a couple of different Fields, We’ll also add them to the “All Items” View with the Add-SPFieldToView.ps1 script. 

Add-SPCurrencyField.ps1

 

You can Add a Currency Field by using the Add-SPCurrencyField.ps1 Script. 


PS > Add-SPCurrencyField -url http://moss -List "My List" -Name Cash -Description "How much Money do you have?"
PS > Add-SPFieldToView -url http://moss -List "My List" -Field Cash -View "All Items"

 

Add-SPDateTimeField.ps1

 

Here’s how we can add a DateTime Field to a List using the Add-SPDateTimeField.ps1 Script. 


PS > Add-SPDateTimeField -url http://moss -List "My List" -Name "Birthday" -Description "When is Your Birthday?"
PS > Add-SPFieldToView -url http://moss -List "My List" -Field Birthday -View "All Items"

 

Add-SPNoteField.ps1

 

The Note Field Accepts Text on Multiple Rows. Here we add a Note Field using the Add-SPNoteField.ps1 Script. 


PS > Add-SPNoteField -url http://moss -List "My List" -Name Description -Description "About Me.."
PS > Add-SPFieldToView -url http://moss -List "My List" -Field Description -View "All Items"

 

Add-SPNumberField.ps1

 

We can also Add Numeric Fields to MOSS. This example shows how to do that using the Add-SPNumberField.ps1 Script. 


PS > Add-SPNumberField -url http://moss -List "My List" -Name Age -Description "How Old Are You?"
PS > Add-SPFieldToView -url http://moss -List "My List" -Field Age -View "All Items"

 

Add-SPYesNoField.ps1

 

With the Add-SPYesNoField.ps1 script, We can add a Yes/No Checkbox Field to our List. 


PS > Add-SPYesNoField -url http://moss -List "My List" -Name "Windows 7" -Description "Do You Have Windows 7 Installed?"
PS > Add-SPFieldToView -url http://moss -List "My List" -Field "Windows 7" -View "All Items"

 

Add-SPUserField.ps1

 

Adding User Fields that point to a Active-Directory User, Can be Added through the Add-SPUserField.ps1 Script. 


PS > Add-SPUserField -url http://moss -List "My List" -Name User -Description "User"
PS > Add-SPFieldToView -url http://moss -List "My List" -Field User -View "All Items"

 

Add-SPMultipleUserField.ps1

 

A User Field can Allow Multiple Selections. Here’s how to add a multiple User Field using the Add-SPMultipleUserField.ps1 script. 


PS > Add-SPMultipleUserField -url http://moss -List "My List" -Name "Multiple Users" -Description "Multiple Users"
PS > Add-SPFieldToView -url http://moss -List "My List" -Field "Multiple Users" -View "All Items"

 

Add-SPMultiChoiceField.ps1

 

Choice Fields can also accept Multiple Choices. Here’s an example on how to add a Multiple Choice Field using the Add-SPMultiChoiceField.ps1 Script. 


PS > Add-SPMultiChoiceField -url http://moss -List "My List" -Name "Favorite Music" -Description "Favorite Music" -Choices $("Country","Metal","Rock","Soul","Jazz")
PS > Add-SPFieldToView -url http://moss -List "My List" -Field "Favorite Music" -View "All Items"

 

Add-SPLookupField.ps1

The Lookup Field Points to an Item in another List. In this Example we’ll Point it to the “Tasks” List.


PS > Add-SPLookupField -url http://moss -List "My List" -Name "My Tasks" -Description "My Tasks" -LookupList Tasks
PS > Add-SPFieldToView -url http://moss -List "My List" -Field "My Tasks" -View "All Items"

 

Add-SPMultiLookupField.ps1

 

LookupFields Can also have multiple Values. We can add a Multiple LookupList through the Add-SPMultiLookupField.ps1 Script. Again, We’ll Use the Tasks List as Lookup. 


PS > Add-SPMultiLookupField -url http://moss -List "My List" -Name "Multiple Tasks" -Description "Multiple Tasks" -LookupList Tasks
PS > Add-SPFieldToView -url http://moss -List "My List" -Field "Multiple Tasks" -View "All Items"

 

Add-SPURLField.ps1

 

Finally, Let’s Add an URL Field to our “Custom List”. 


PS > Add-SPURLField -url http://moss -List "My List" -Name HomePage -Description "HomePage"
PS > Add-SPFieldToView -url http://moss -List "My List" -Field HomePage -View "All Items"

 

Now, Let’s see what our List Looks like. 

moss15 

Let’s Add Two Task Items and then create a New Item in our “Custom List” using the Set-SPItem.ps1 Script. 


PS > Set-SPItem -url http://moss -List Tasks -Name "My First Task" -Values @{"Priority" = "(3) Low"; "Status" = "In Progress"; "% Complete" = "0.4"; "Assigned To" = "powershell\Administrator"; "Description" = "My First Task"; "Start Date" = "09/07/09"; "Due Date" = "09/08/09"}
PS > Set-SPItem -url http://moss -List Tasks -Name "My Second Task" -Values @{"Priority" = "(1) High"; "Status" = "Not Started"; "% Complete" = "0"; "Assigned To" = "powershell\Administrator"; "Description" = "My Second Task"; "Start Date" = "09/07/09"; "Due Date" = "09/08/09"}

 

Here’s our New Tasks up and running. 

moss16 

Now Let’s Add a New Item in our “Custom List”. Note that Fields that Allow multiple Values are divided by a ; in the command below. For Instance, if I want to two values to a multiple Choice Field I would have to Type: @{“MultiChoiceField” = “Value1; Value2; Value3″}.


PS > Set-SPItem -url http://moss -List "My List" -Name "My First Item" -Values @{"Cash" = "10"; "Birthday" = "05/05/1982"; "Description" = "My First Entry"; "Age" = "27"; "Windows 7" = "Yes"; "User" = "powershell\administrator"; "Multiple Users" = "powershell\administrator; powershell\nigo"; "Favorite Music" = "Metal; Country"; "My Tasks" = "My First Task"; "Multiple Tasks" = "My First Task; My Second Task"; "HomePage" = "http://www.powershell.nu; My Blog" }

 

And Here’s what our Item Looks like in MOSS 2007. 

moss17 

Set-SPView.ps1

Let’s Modify the Apperance of the “All Tasks” View in the Tasks List so that it Groups By Status. When Modifying appearance of a View, We change the View Query. This can be Done by using the Set-SPView.ps1 Script.


PS > Set-SPView -url http://moss -List Tasks -View "All Tasks" -Query ''

 

Here’s what our Tasks Look like now. 

moss18 

Remember the “Shared Documents that We Added Fields to earlier ? Let’s Group the “All Documents” View by one of our New Fields. Note that the Name=”Document_x0020_Type” in the Query. It’s because the Field has Spaces in its Name. 


PS > Set-SPView -url http://moss -List "Shared Documents" -View "All Documents" -Query ''

 

And now our Documents are Grouped by “Document Type”. 

moss19 

Add-SPSpite.ps1

 

With the Add-SPSite.ps1 Script, we can add New Sites to our Site Collection. There are a Couple of Different Templates that we can use. To get a list of all templates available, SImply type the following commands. 


PS > $OpenWeb = Get-SPWeb http://moss; $OpenWeb.GetAvailableWebTemplates(1033) | Select Name; $OpenWeb.Dispose()

 

Let’s Create a New Site For our IT Department. 


PS > Add-SPSite -url http://moss -weburl "IT" -Title "Information Technology" -Description "IT Department" -Template "STS#1"

 

Here’s What the Site looks like now. 

moss20 

Since we used a STS#0 Template, the Site doesn’t contain any Lists or information. 

Add-SPGroup.ps1

 

If we want to Add a New Group to our Site Collection we can use the Add-SPGroup.ps1 Script. Here’s an example on using the Script. 


PS > Add-SPGroup -url http://moss -Group "New Group" -Role Read -Owner "powershell\administrator"

 

Now our “New Group” is Added to our Site with “Read” Permissions 

moss21 

Add-SPUser.ps1

 

With our New Group set up, we can go ahead and add users to it. To accomplish this, we can use the Add-SPUser.ps1 Script. Here’s an example on adding a User to the “New Group”.


PS > Add-SPUser -url http://moss -Group "New Group" -Domain "powershell.nu" -sAMAccountName "goude" -mail "niklas.goude@zipper.se" -FullName "Niklas Goude"

 

If we look in the Group now, our User will be added. 

moss22 

Add-SPSitePermission.ps1

 

Let’s Give our Group Full Permissions on The IT Site that we created earlier.


PS > Add-SPSitePermission -url "http://moss/IT" -Group "New Group" -Permission "FullMask"

 

Now all users in the Group have Full Permissions on the “IT” Site. On the RootWeb, they still have Read Permissions. 

Add-SPImageWebPart.ps1

 

Let’s Add a New Image WebPart to the IT Site that we just created. Here’s an example on how to do it using the Add-SPImageWebPart.ps1 Script.


PS > Add-SPImageWebPart -url http://moss/IT -Name "My Image" -Image "C:\Images\PowerShell.jpg" -ChromeType "None"

 

Now the Image WebPart is Added to the Site. There are a couple of parameters you can use with this script. simply type Add-SPImageWebPart -help to check out available parameters. 


PS > Add-SPImageWebPart -help

 

moss23 

Add-SPListViewWebPart.ps1

 

The Add-SPListViewWebPart.ps1 let’s us Add list View WebParts to our Sites. The Script contains a couple of parameters that let’s you choose Chrome Type, Zone and a couple of other things. Here’s an example on Using the Script.


PS > Add-SPListViewWebPart -url http://moss -List "My List" -Name "Client Statistics" -ChromeType "None"

 

Here’s our New List WebPart in Action! 

moss24 

Remove-SPField.ps1

 

If you want to Remove a Field From a List you can use the Remove-SPField.ps1 Script. Here’s an example on how to use the Script.


PS > Remove-SPField -url http://moss -List "Shared Documents" -Field "Document Type"

 

Remove-SPItem.ps1

 

If you want to Remove a List Item you can use the Remove-SPItem.ps1 script. The -name parameter matches with the Title field by default. If you want to match the name with another field, simply use the -field parameter.


PS > Remove-SPItem -url http://moss -List Tasks -name "My First Task"
PS > Remove-SPItem -url http://moss -List Tasks -name "Not Started" -Field Status

 

Remove-SPList.ps1

 

If you want to Remove an entire List you can use the Remove-SPList.ps1 script,


PS > Remove-SPList -url http://moss -List Tasks

 

This Command Removed the Tasks List from the RootWeb Site. 

Remove-SPSite.ps1

 

When Removing a Site from MOSS 2007 You can use the Remove-SPSite.ps1 script. This exmaple shows how to remove the IT Site that we created earlier.


PS > Remove-SPSite -url http://moss/IT

 

Export-SPSite.ps1

 

Let’s take a Backup of our Site Collection. We can either do this through Central Administration, through an STSADM Command or by Using PowerShell. This Example shows how to use a PowerShell Script to take a Backup of a Site Collection.


PS > Export-SPSite -url http://moss -file Backup.bak -Location C:\Backup\

 

The Command Places a Backup file in C:\Backup\Backup.bak. to automate this you could set up a Scheduled Task that takes a backup at specified weekdays. 

Import-SPSite.ps1

 

Now That we have a Backup, Let’s restore our Site Collection from the Backup file. To achieve this, we can use the Import-SPSite.ps1 Script. Here’s an example on how to use the script.


PS > Import-SPSite -url http://moss -file Backup.bak -Location C:\Backup\

 

Here’s a List of All scripts from this Post. Enjoy! 

Regards

Goude 

 

Rating 3.50 out of 5
[?]

35 thoughts on “MOSS 2007 Script Collection

  1. Hi dude, thanks for sharing and for what you show us at the usergruoup! Have just installed PS and played with your script. Btw, is there a PS-way to set a theme to all sites in a sitecol at once, no matter what ever they are teamsites, publishing sites and so on?

  2. Pingback: Möte med SharePoint Community SVerige | Codedstyle

  3. Pingback: Tune Up Your PC » Post Topic » Möte med SharePoint Community SVerige

  4. I have looked at a LOT of script examples like this online.
    This is without doubt the best and most useful helpful and complete set of PS scripts i have seen yet for sharepoint.

    Many many thanks
    Allan

  5. Hi Christian,

    You could always loop through the Site in your Site Collection with the Get-SPSite.ps1 Script and the Set-SPTheme Script. here’s an example:

    PS > $SPSite = Get-SPSite -url http://moss
    PS > $SPSite.AllWebs | ForEach { Set-SPTheme -url $_.url -Theme “Reflector” }

    Hope this helps

    //Goude

  6. Pingback: Workflow Automation with PowerShell « The Art of SharePoint Ninjutsu

  7. Hi Niklas,
    Thank you for sharing this script, they were very useful, but I was wondering if you have scripts for adding lookup field in Share Point 2010? I have a problem with adding lookup field with more fields from lookup list, which is the new feature in 2010 version.

  8. Hi Goude,

    Could you please tell me a way to actually get all the permissions(site permissions, objects, alerts, tasks or watever permissions there are in the environment. We have AD accounts in MOSS to store all user, groups and permissions information) in the MOSS environment by writing a script? Is it even possible with Powershell scripts or not?

    Thanks
    Vishwas

  9. Small correction on SPFolder, as it doesn’t allow option to add sub-folders to document library..
    i.e. Folder in Folder in Folder

    function Add-SPFolder([string]$url, [string]$List, [string]$FolderUrl,[string]$Name) {

    $OpenWeb = Get-SPWeb $url
    $OpenList = $OpenWeb.Lists[$List]

    if($FolderUrl -eq “”)
    {
    #$Folder = $OpenList.Folders.Add(“http://sp2010/sites/client3/matter0/a”,[Microsoft.SharePoint.SPFileSystemObjectType]::Folder,$Name)
    $Folder = $OpenList.Folders.Add(“”,[Microsoft.SharePoint.SPFileSystemObjectType]::Folder,$Name)
    }
    else
    {
    $Folder = $OpenList.Folders.Add(“$url/$List$FolderUrl”,[Microsoft.SharePoint.SPFileSystemObjectType]::Folder,$Name)
    }

    $Folder.Update()

    $OpenWeb.Dispose()
    }

  10. Is it possible to use the Add-SPLink.ps1 script with an excel spreadsheet as the source for the link information?

  11. Of course.

    Easiest way is to save the excel spreadsheet as a .csv file and use Import-Csv and pipe to ForEach-Object to loop through each row in the csv file and run the Add-SPLink.ps1 script. ex:

    #Csv File
    Link, Description, Note
    http://www.powershell.nu, A PowerShell Blog, Cool
    http://www.powershell.com, Community, Great community

    Import-Csv C:\MyExcelFile.csv | ForEach-Object {
    Add-SPLink -url http://moss -List “Links” -Link $_.Link -Description $_.Description -Notes $_.Notes
    }

  12. Pingback: Blog Post: Emptying the Second Stage Recycle Bin in SharePoint 2007 | IT.beta

  13. Thank you very much for these. I have learned a lot from reading your code. I am having a problem with the Add-SPListViewWebPart.ps1 that I hope you can help with. I get the following error when I run it:

    Exception calling “AddWebPart” with “3″ argument(s): “This page has encountered a cri
    tical error. Contact your system administrator if this problem persists.”
    At line:74 char:28
    + $WebPartManager.AddWebPart <<<< ($ListViewWebPart,$Zone,$Index)
    + CategoryInfo : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : DotNetMethodException

  14. Thanks very much for this compendium.

    A small question : Is there a script to create Site collections ?

  15. Hello,
    The function there are amazing! thx
    I am working on a Powershell script to change ”Created By” and ”Created Date” from a list of type discussion board in MOSS2007.
    When I use the Get-SPItem and use the -All switch it work great. When I filter by ID (taken from the previous command result) it works fine. When I search by Title and Name taken from the 1st result list it does not find it. It could be useful for me by subject/Title as I do not have the ID for my search. The second problem is having error when updating fields. thx for help

  16. Hi

    Do we have any built-in out-of-the-box cmdlets in powershell for MOSS 2007 like we have for SP2010?

  17. Niklas, the scripts did not work for me on SP 2007. I get the following error:
    New-Object : Cannot find type [Microsoft.SharePoint.SPSite]: make sure the assembly containing this type is loaded.
    + New-Object <<<< Microsoft.SharePoint.SPSite($url)
    + CategoryInfo : InvalidType: (:) [New-Object], PSArgumentExcepti
    on
    + FullyQualifiedErrorId : TypeNotFound,Microsoft.PowerShell.Commands.NewOb
    jectCommand

  18. Hi Niklas! Thanks a lot for this great work.
    I have some error after executing Get-SPList function. It return the number of items in a list and bellow the number is the error. It seems it doesn’t matter on anything but just wanted to ask if it can be resolved.

    PS C:\> $SPList = Get-SPList -url http://project/PWA/TestSite -List Test2; $SPList.ItemCount; $SPList.Dispose()

    2
    [Microsoft.SharePoint.SPList] doesn’t contain method called “Dispose”
    $SPList.Dispose <<<< ()
    + CategoryInfo : InvalidOperation: (Dispose:String) [], RuntimeException
    + FullyQualifiedErrorId : MethodNotFound

  19. Hi Niklas, I wondered if you’d ever tried to change the value of the NoCrawl property on a SP List from $true to $false using PowerShell in a MOSS 2007 Document Library? I’ve been trying it and haven’t been able to acheive it… I attach an extract of my code below

    $site = New-Object Microsoft.SharePoint.SPSite($url)
    $web = $site.OpenWeb()
    $doclib = $web.Lists[$list]
    $doclib.NoCrawl = $false
    $web.Dispose()
    $site.Dispose()

    This runs without error or exception, but has no effect on the value of NoCrawl. I tried adding $doclib.Update() immediately after setting the value of $doclib.NoCrawl, but that results in an exception as below…

    Exception calling “Update” with “0″ argument(s): “Object reference not set to an instance of an object.”

    Any suggestions?

  20. Hi,
    Thanks a lot for sharing the above scripts…….Require your expert help for deleting a folder in a list… Please share the powershell script to delete a folder in calendar list…..i would be great full…. I searched a lot but cound’nt find anything relevant…..

  21. Hi!

    How can I rewrite this for SharePoint 2007?

    $spWeb = Get-SPWeb -Identity http://server/subsite
    $spList = $spWeb.Lists["Display_list_name"]
    $spList.Items | ForEach-Object { $_.Update() }

    My intent is to add this powershell script to my task scheduler and invoke list update to updated all of the calcaluted columns items that is using Today’s date (static).

    Thank you.

    Dave

  22. Doesn’t look like anyone is getting many answers here but thought I would ask anyway. How would I get another property like permission?
    and the obvious follow up, how can I add a permission to a item.

  23. Hi,

    Could you please let me know, from a web app how i can pull list of all Site Collections and their associated site collection admins

    More likely looking for a report that includes
    Site Collection Name
    Site URL (Site Collection URL)
    Type (Primary or Secondry)
    Login (Primary SC Admin AD\ID)
    Name (Primary SC Admin Name)
    Email (Primary SC Admin Email address)

    Thank you for your help

  24. Hi,
    Great Script. I have a need to insert content types. Is there a way to insert Content Types/Fields using PowerShell into Sharepoint 2007 (MOSS)?

    Thank You.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Anti-Spam Protection by WP-SpamFree