Exports Site Columns Using Powershell

Hi

Here is I am  Explain you  how to export site column using Power shell

Open your notpade++  and copy past below code inthe file save file as .ps1

in this powerhsell code i have generate log file you can notice it from code snippet

just give Export location as per your convince

I assume that you are aware of how to run power shell script in SharePoint managment Shell

pass webUrl as parameter

Remarks-Change groups name with your Group name which you want toin Code


#Necessary Parameters
#Necessary Parameters
[CmdletBinding()]
param 
(
	[string] $Web
)
#/// <summary>
#/// Remove Default Group Permission
#/// </summary>
Function ExportSiteColumnXml([string] $web)
{ 
    try
	{  
      		if($web -ne "")
			{  
              
			    $sourceWeb = Get-SPWeb $web
				$xmlFilePath = "C:\Script-SiteColumns.xml"

				#Create Export Files
				New-Item $xmlFilePath -type file -force

				#Export Site Columns to XML file
				Add-Content $xmlFilePath "<?xml version=`"1.0`" encoding=`"utf-8`"?>"
				Add-Content $xmlFilePath "`n<Fields>"
				$sourceWeb.Fields | ForEach-Object {
					if ($_.Group -eq "Your Site Columns Group Name") {
						Add-Content $xmlFilePath $_.SchemaXml
					}
				}
				Add-Content $xmlFilePath "</Fields>"	                            
             }
             else
             {
                 Write-Host "Please provide proper parameters" -ForegroundColor Red
				LogInfo "Please provide proper parameters"
             }
    }
    catch
    {
      Write-Host "Something went wrong. Please check Logs for additional information" -ForegroundColor Red
        LogError $_

    }
    finally
    { 
        #Dispose your site when process end Successivly
		 if($sourceWeb -ne $null)
			{
				$sourceWeb.Dispose();
			}
			LogInfo "Process Ended"
    }
  

}

#/// <summary>
#/// Prints errors if generated
#/// </summary>
Function LogError($message)
{
     $timeStamp = Get-Date -format "dd/MM/yyyy HH:mm:ss"
     $outContent = "$timeStamp`tError`t`t$message`n"
     Add-Content $Logfile $outContent
}

#/// <summary>
#/// Prints logs in log file
#/// </summary>
Function LogInfo($message)
{
     $timeStamp = Get-Date -format "dd/MM/yyyy HH:mm:ss"
     $outContent = "$timeStamp`tInfo`t`t$message`n"
     Add-Content $Logfile $outContent
}




#/// <summary>
#/// Load the SharePoint Snapin 
#/// </summary>
#/// <returns>
#/// </returns>
Function Load-SharePoint-Powershell
{ 
    Write-Host "TEST"
	if ( (Get-PSSnapin -Name Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue) -eq $null )
	{    
	      Add-PsSnapin Microsoft.SharePoint.PowerShell -ErrorAction Stop
          
	}
     
}

$Logfile = "c:\RTED\RTED-$(get-date -f MMddyyyy_HHmmss).log";
Load-SharePoint-Powershell
ExportSiteColumnXml $web


&nbsp;

3 thoughts on “Exports Site Columns Using Powershell

Leave a comment