In this article we will be seeing how to add a new custom site column and modify
the existing site column in SharePoint using powershell scripts.
Adding a new custom site column using powershell script
$site = Get-SPSite -Identity "http://serverName:4001/"
$web = $site.RootWeb
$fieldXML = '<Field Type="Text"
Name="CustomSiteColumn"
Description="This is a Custom Site Column."
DisplayName="Custom Site Column"
Group="Custom Site Column"
Hidden="FALSE"
Required="FALSE"
ShowInDisplayForm="TRUE"
ShowInEditForm="TRUE"
ShowInListSettings="TRUE"
ShowInNewForm="TRUE"></Field>'
$web.Fields.AddFieldAsXml($fieldXML)
$web.Dispose()
$site.Dispose()
(OR)
$site = Get-SPSite -Identity "http://serverName:4001/"
$web = $site.RootWeb
$web.Fields.Add("CustomSiteColumn",[Microsoft.SharePoint.SPFieldType]::Text,$true)
$web.Dispose()
$site.Dispose()
A new custom site column is created successfully using the above script.
Modify the existing column property using powershell script
$site = Get-SPSite -Identity "http://serverName:4001/"
$web = $site.RootWeb
$field=$web.Fields["Custom Site Column"]
$field.Type= "Choice"
$field.Update($true)
$web.Dispose()
$site.Dispose()
The property of the existing site column is changed from "Text" to "Choice".