Rana Omer Hussain's RegEdit

My Weblog registry is a directory which stores settings and options for my Professional Life

Archive for the tag “PowerShell”

Change the Welcome Page / Homepage in SharePoint 2010 Programmatically

When exporting and importing sites throughout SharePoint 2010 that are upgraded sites from SharePoint 2007, you probably noticed that the homepage for the imported site is defaulted to SharePoint 2010′s wiki-style page at /SitePages/Home.aspx. However, until you start using these new pages you may want to revert or keep the homepage as the default.aspx page.

To do this manually, you need to go to Site Actions > Site Settings > Look and Feel > Welcome Page.

However, this Welcome Page link is only available for those sites where the Publishing feature is activated at the Site Collection and Site level.

This could be pretty tedious if you have to do this for every site in the web application. So, if you need to do this fast for multiple sites and don’t have direct access to the Welcome Page link, what do you do? <pausing for dramatic effect> You use Power-Shell!

Below is a script I created that will prompt to enter in the Web Application and Welcome Page and then loop through each site in the Web Application and set the Welcome Page. Save this as a .ps1 file and run it from the SharePoint 2010 Management Shell:

$webapp = Read-Host "Enter Web Application"

$welcomepage = Read-Host "Enter Welcome Page"

$webs = Get-SPSite -WebApplication

$webapp -Limit All | Get-SPWeb -Limit All

foreach ($web in $webs){

$rootFolder = $web.RootFolder

$rootFolder.WelcomePage = $welcomepage

Write-Host "Setting"$web.Title"homepage to"$welcomepage    $rootFolder.Update()

$web.Dispose()

}

Now, navigate back to a site and see that the homepage is now set to the given page!

Delete All Old Versions from SharePoint 2010 Document Libraries

How many versions of a typical document do you need to keep?  5?  10?  100?
SharePoint content databases often get cluttered with redundant versions of the same documents.  In some cases, I’ve seen presentation libraries with less than 10 GB of active content, but hundreds of GBs of content due to gradual changes.
If you’re ready to clear out old versions, SharePoint 2010′s Management Shell makes it easy.  The script below will iterate through all lists and update them to keep only the last 2 copies.  It will also loop through and delete unneeded versions.

Get-SPWebApplication |  Get-SPSite -Limit All |  Get-SPWeb -Limit All |  ForEach-Object  { ForEach($list in $_.Lists) { If($list.EnableVersioning -eq $true) { $list.MajorVersionLimit = 2; $list.Update(); ForEach($item in$list.Items) { $item.URL; $item.SystemUpdate() } } } }

You can see that it…
  1. Loops through all web applications
  2. Loops through their site collections
  3. Loops through their webs
  4. Loops through their lists
  5. If versioning’s enabled, it sets the major version limit to 2
  6. In order to remove old versions, it needs to loop through each item and perform a system update

The script can be easily amended to deal with only specific site collections / libraries.  To keep a different number of versions, modify the MajorVersionLimit variable above.

PowerShell Demo Tools

Several people who attended my PowerShell talk at SPC09 have been asking for the “Start-Demo” Script I used to demo PowerShell commands without having to type them in front of a live audience.  

The script I used is really just a slightly modified version of this one 

http://blogs.msdn.com/powershell/archive/2007/06/03/new-and-improved-start-demo.aspx 

iI recommend just using the version provided by the PowerShell team.

Also, I recommend pairing this with the Sysinternals tool ZoomIt to allow for zooming and underlining elements on the screen.

This is a free tool available from the Sysinternals website: http://technet.microsoft.com/en-us/sysinternals/default.aspx

PowerShell Scripting Editor

If you’re anything like me, you like to script in an organized fashion. Notepad just doesn’t quite do it for me, so be sure to check out the new PowerGUI Script Editor (comes with PowerGUI), which has built in intelli-sense and color coding!

List All SharePoint 2010 PowerShell Commands

In order to master SharePoint 2010, you will have to get comfortable utilizing PowerShell (aka SharePoint 2010 Management Shell). There are so many commands (or command-lets as they are called) that interact with SharePoint that it makes it nearly impossible to remember them all. Use this PowerShell command to output a list of all cmdlets that are found within the SharePoint 2010 namespace:

Get-Command –PSSnapin “Microsoft.SharePoint.PowerShell” | format-table name > C:\SP2010_PowerShell_Commands.txt

If you would like a little more detail, then try this one:

Get-Command –PSSnapin “Microsoft.SharePoint.PowerShell” | select name, definition | format-list > C:\SP2010_PowerShell_Commands.txt

Note: You may also type gcm as the alias for Get-Command.

Ref : List All SharePoint 2010 PowerShell Commands By Adam Preston

Post Navigation

Follow

Get every new post delivered to your Inbox.

Join 321 other followers