Monday, 9 August 2010

Find GUID of a site column in SharePoint using PowerShell

Whilst a simple script, this illustrates the real advantages of PowerShell. I needed to find the ID’s of some site columns I had created in SharePoint 2010. In the days of MOSS 2007, I would have had to either work out the GUID from looking at the URL of the column in the browser or write a custom application in Visual Studio to get the information I needed by accessing the Object Model. Now, I spend a minute writing a script in PowerShell and it’s reusable time and time again.

The script asks the user to input the display name of the site column and then gets the name and GUID of the column and outputs it to the console. I have configured the URL as a variable, but you could also ask the user for this if you wanted to:

$web = Get-SPWeb http://portal
$columnName = Read-Host "Enter the column title (display name)"
write-host $web.Fields[$columnName].Title -nonewline
write-host " "$web.Fields[$columnName].Id -foregroundcolor Red
$web.Dispose()

You could use the essence of this script to find a whole bunch of information about the column, if required. A full list of properties can be found on MSDN here.

10 comments:

  1. $web = Get-SPWeb http://sp2010dev
    foreach ($field in $web.Fields)
    {
    write-output "Title: $($field.Title) | ID: $($field.Id)"
    }
    $web.Dispose()

    Save it to a file. Then run with something like ./fields.ps1 out-File fields.txt

    Just creates a text file with all the site columns and ids.

    Thanks for posting!

    ReplyDelete
  2. sorry... just make sure you swap http://sp2010dev for the appropriate AAM.

    ReplyDelete
  3. Brilliant. Just what I was looking for. Thanks guys.

    ReplyDelete