Add Site Columns using Powershell

  1. Add - PSSnapin "Microsoft.SharePoint.PowerShell"#  
  2. Get site and web object  
  3. $site = Get - SPSite - Identity "http://siteurl"  
  4. $web = $site.RootWeb  
  5.   
  6. function CreateSiteColumn($ColName, $DisplayName, $DataType) {#  
  7.     Assign fieldXML variable with XML string  
  8.     for site column  
  9.     $fieldXML = @ " <  
  10.         Field Type = "$DataType"  
  11.     Name = "$ColName"  
  12.     Description = "Site Column created by using Powershell with format of $DataType column."  
  13.     DisplayName = "$DisplayName"  
  14.     StaticName = "$colName"  
  15.     Group = "Calpine"  
  16.     Hidden = "FALSE"  
  17.     Required = "FALSE"  
  18.     Sealed = "FALSE"  
  19.     ShowInDisplayForm = "TRUE"  
  20.     ShowInEditForm = "TRUE"  
  21.     ShowInListSettings = "TRUE"  
  22.     ShowInNewForm = "TRUE" > < /Field>  
  23.     "@#  
  24.     Output XML to console  
  25.     write - host $fieldXML# Create site column from XML string  
  26.     $web.Fields.AddFieldAsXml($fieldXML)# Dispose of Web and Site objects  
  27.     $web.Dispose()  
  28. }  
  29. CreateSiteColumn "MultiLineText"  
  30. "Multi Lines Text"  
  31. "Note"  
  32. CreateSiteColumn "DateTime"  
  33. "Date Time"  
  34. "DateTime"  
  35. CreateSiteColumn "SingleLineText"  
  36. "Single line of Text"  
  37. "Text"  
  38. CreateSiteColumn "YesNo"  
  39. "Yes NO"  
  40. "Boolean"