Monday, March 25, 2013

Export Import Site Columns with PowerShell

Its a frequent requirement to copy site columns between SharePoint environments, isn't it? Today had a requirement to copy bunch of site columns from development environment to staging environment.

We used to package site columns as a WSP Solution package in such requirements: Create Site Column Feature for SharePoint 2010 ,This time, Lets use PowerShell to Export and Import Site Columns!

PowerShell Script to Export Site columns:

    #Get the Source Web
    $sourceWeb = Get-SPWeb ""
    #Create a XML File to Export Fields
    $xmlFile = "C:\SiteColumns.xml"
    New-Item $xmlFile -type file -force

    #Wrap Field Schema XML inside <Fields> Element 
    Add-Content $xmlFile "`n<Fields>"
    #Export All Site Columns of specific Group to XML file
    $sourceWeb.Fields | ForEach-Object {
        if ($_.Group -eq "Crescent Travel Request") {
            Add-Content $xmlFile $_.SchemaXml
    #Closing Wrapper
    Add-Content $xmlFile "</Fields>"
    #Dispose the web object

PowerShell Script to Import Site Columns from XML:
  #Get the Target Web
  $TargetWeb = Get-SPWeb ""
  #Get XML file exported
  [xml]$fieldsXML = Get-Content("C:\SiteColumns.xml")
  #Loop Through Each Field
  $fieldsXML.Fields.Field | ForEach-Object {
  #Check if the target web has the field already!
  if ($TargetWeb.Fields.ContainsField( $_.Name) -Eq $false){ 
   #Add Site column to Target Web
Thanks Phil for the idea:

You might also like:
SharePoint Usage Reports
Usage reports, collaboration and audit for SharePoint.
Document SharePoint Farm
Automatically generate SharePoint documentation.

Check out these SharePoint products:


  1. Does not work for me... Empty XML...

    1. Check your Site columns Group! I've used "Crescent Travel Request", Update it accordingly!!

  2. thanks, the only script that worked for me :)

  3. How can I restrict the export only the selected column from the group? Not all the columns of the entire group?

  4. I did try to put the if {$_.Name -eq "ColumnName" } within the if ($_.Group -eq "GroupName"). But it is not exporting the selected column.

    1. Khushi,

      There is no "Name" property in SPField class. Use properties such as: Title, StaticName, InternalName.

  5. Thank you so much Salaudeen!! I tried $_.Name didn't work I tried $_.DisplayName didn't work!
    You are the Best StaticName did work!!!

    Appreciate your help.

  6. Exactly what I was looking for!!

  7. Thank you.
    You didn't dispose web in import script ;)

    #Dispose the web object

  8. Thank you a million!
    I sincerely appreciate your help.


Please Login and comment to get your questions answered!

You might also like:

Related Posts Plugin for WordPress, Blogger...