4

I'm looking for a powershell script that allows me to set the default value for a column in SharePoint. Thanks

9

The main point here is that you can work with SharePoint Object Model from PS. So, your task is just to find out the way to change default field value using SharePoint Object Model, and translate it to PS notation.

The default value for a column in SharePoint object model is set using SPField.DefaultValue property.

So, let's try use this knowledge in PS. I have SampleList2 of type Contacts on my local SharePoint portal, and will try to change default value for a 'First Name' field.

Below is my PowerShell script:

$w = Get-SPWeb http://localhost
$l = $w.GetListFromUrl("/Lists/SampleList2/AllItems.aspx")
$f = $l.Fields["First Name"]
$f.DefaultValue = "my default name"
$f.Update()

Let's try to create new item using GUI now:

enter image description here

Enjoy! :)

0

Check this blog post Use PowerShell to Manage Lists, Views, and Items in SharePoint on how to update SPListItem via PowerShell.

Sample:

We can assign additional values to fields as well. In the next example, we add values to the TextField and ChoiceField, which we created earlier in this example:

PS > $spListItem["TextField"] = "Hey hey" 

PS > $spListItem["ChoiceField"] = "First Choice"
#Finally, we use the Update() method to create the new list item:
PS > $spListItem.Update()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.