1

Hello I am working on a powershell script to update the default values for a specific column (Year) from 2016 to 2017. We have several project sites (each projectsite is a sitecollection). I would like to update all the column values for all the sites in the different site collections

I have created the following, but isn't working:

Add-PSSnapin Microsoft.Sharepoint.Powershell
$site = Get-SPSite "http://xxx.westeurope.cloudapp.azure.com/sites"
$sitelists = foreach ($web in $site.AllWebs) {
foreach($list in $web.lists){ $list }}
$list = $Web.Lists["Documents"]
$field = $list.Fields["Year"]
$field.DefaultValue = "2017"
$field.Update()
1
  • Why are you trying to store a loop in a variable? Commented Mar 21, 2017 at 12:41

1 Answer 1

2

This works:

$w = Get-SPWeb "http://sharepoint"
$l = $w.lists["Shared Documents"]
$f = $l.Fields["Year"]
$f.DefaultValue = "2017"
$f.Update()

Or this:

if ((Get-PSSnapin -Name Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue) -eq $null)
{
    Add-PsSnapin Microsoft.SharePoint.PowerShell
}

$site = Get-SPSite "http://sharepoint"
foreach($web in $site.AllWebs) 
{
    foreach($list in $web.lists){
        $list = $Web.Lists["Documents"]
        $field = $list.Fields["Year"]
        $field.DefaultValue = "2017"
        $field.Update()
    }
}

One more example:

$allWebs = Get-SPWebApplication "http://yourWebAPP" | Get-SPSite -Limit All | Get-SPWeb -Limit All 

foreach($web in $allWebs)
{
     foreach($list in $web.lists){
        $list = $Web.Lists["Documents"]
        $field = $list.Fields["Year"]
        $field.DefaultValue = "2017"
        $field.Update()
    }
}

You will want to add error handling, checking if objects exists, and disposing of objects.

5
  • Is not working, we have a web-application XXX.westeurope.cloudapp.azure.com within that webapp we have several sitecollections. Get the following error: The property 'DefaultValue' cannot be found on this object. Verify that the property exists and can be set.
    – Webmagic
    Commented Mar 21, 2017 at 12:59
  • Try the first example on a single Web, then let me know. Commented Mar 21, 2017 at 13:13
  • This is working for one specific site collection Add-PSSnapin Microsoft.Sharepoint.Powershell $w = Get-SPWeb "xxx.westeurope.cloudapp.azure.com/sites/project1" $l = $w.lists["Documents"] $f = $l.Fields["Year"] $f.DefaultValue = "2017" $f.Update()
    – Webmagic
    Commented Mar 21, 2017 at 13:24
  • Then a point to a specific sitecollection, but would like to use the script to walk through all my site collections. Thanks in advance.
    – Webmagic
    Commented Mar 21, 2017 at 13:25
  • Added another example that gets the web app, sites, and webs. After that, it will loop through the webs, then lists. Commented Mar 21, 2017 at 13:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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