How to Add a Column to a SharePoint List using PowerShell?
Requirement: Add columns to the SharePoint list using PowerShell.
PowerShell Script to Add Columns to SharePoint List:
Do you need to add a column to a SharePoint list, but don’t want to do it manually? In this blog post, I will walk you through the steps of adding a column to a list using PowerShell.
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
Function Add-FieldToList($SiteURL,$ListName, $FieldName, $FieldType, $IsRequired)
{
#Set the Error Action
$ErrorActionPreference = "Stop"
Try{
#Get the List
$List = (Get-SPWeb $SiteURL).Lists.TryGetList($ListName)
#Check if List with specific name exists
if($List -ne $null)
{
if(!$List.Fields.ContainsField($FieldName))
{
#Add columns to the List
$List.Fields.Add($FieldName,$FieldType,$IsRequired)
#Update the List
$List.Update()
#Update the default view to include the new column
$View = $List.DefaultView # OR $List.Views["All Items"]
$View.ViewFields.Add($FieldName)
$View.Update()
write-host "New Column '$FieldName' Added to the List!" -ForegroundColor Green
}
else
{
write-host "Field '$FieldName' Already Exists in the List" -ForegroundColor Red
}
}
else
{
write-host "List '$ListName' doesn't exists!" -ForegroundColor Red
}
}
catch {
Write-Host $_.Exception.Message -ForegroundColor Red
}
finally {
#Reset the Error Action to Default
$ErrorActionPreference = "Continue"
}
}
#Parameters
$SiteURL="https://intranet.crescent.com/"
$ListName = "Customer Directory"
#Add 'Name' - Single Line of Text Field
$FieldType = [Microsoft.SharePoint.SPFieldType]::Text
$FieldName="Name"
$IsRequired = $False
#Call the function to Add Field to List
Add-FieldToList $SiteURL $ListName $FieldName $FieldType $IsRequired
#Add Phone Number - Number Field
$FieldType = [Microsoft.SharePoint.SPFieldType]::Number
$FieldName="Phone Number"
$IsRequired = $False
#Call the funtion to Add Field to List
Add-FieldToList $SiteURL $ListName $FieldName $FieldType $IsRequired
#Date of Joing - Date Field
$FieldType = [Microsoft.SharePoint.SPFieldType]::DateTime
$FieldName="Date of Join"
$IsRequired = $False
#Call the funtion to Add Field to List
Add-FieldToList $SiteURL $ListName $FieldName $FieldType $IsRequired
You can also add fields to the SharePoint list from their field schema. Here is how:
#Field Schemas
$NameFldSchema="<Field Type='Text' DisplayName='Name' Required='False' MaxLength='255' StaticName='Name' Name='Name' />"
$PhoneNoFldSchema="<Field Type='Number' DisplayName='Phone Number' Required='False' MaxLength='255' StaticName='PhoneNumber' Name='PhoneNumber' />"
$DOBFldSchema="<Field Type='DateTime' DisplayName='Date of Birth' Required='False' MaxLength='255' StaticName='DateOfBirth' Name='DateOfBirth' /> "
#For Field schemas, Refer: https://msdn.microsoft.com/en-us/library/office/aa979575(v=office.15).aspx
#Add Columns to the List
$List.Fields.AddFieldAsXml($NameFldSchema, $True,[Microsoft.SharePoint.SPAddFieldOptions]::AddFieldToDefaultView)
While the above script is fairly simple, to add other types of fields such as choice fields, lookups, etc., refer to the below posts:
- How to Add Choice Field to SharePoint list using PowerShell?
- PowerShell to Create new Single-Multiple Lines of Text Field to SharePoint List
- Add Date Time column to SharePoint List using PowerShell
- How to add lookup field to SharePoint List with PowerShell?
- How to Add Managed Metadata Column to SharePoint List using PowerShell?
Thanks mate, all the best!
Hi Salaudeen
Thanks for this script, it is appreciated.
I just can’t get it to work for a “Person or Group” column.
I tried:
$FieldType = [Microsoft.SharePoint.SPFieldType]::Person
$FieldType = [Microsoft.SharePoint.SPFieldType]::PersonGroup
Nothing seems to work.
Thanks again
Marlin
Try the FieldXML Method: $FieldXml = “ “
Hello,
can you explaint how to ad “FieldXML Method: $FieldXml for “Person or Group” to this script in up site?
I´m new in powershell. I have the same problem with add Person and Group Column and set parameters fields. Thx for help.