2

I am attempting to write a powershell script that will iterate through all document libraries and provide me with the default values for specific columns.

I am successfully iteration through the sites, and their respective lists, but for a column named "NH Department" I would like to know if a default value has been set or not, and if so, what the default value is. I have tried the following with no success (partial, applicable code below):

foreach ($list in $subWeb.Lists)
{
  $listname = $list.Title

  $FieldName = $list.Fields["NH Department"]
  Write-Output $("NH Department: " + $FieldName.value)

}

I guess I don't know how to query the VALUE of the field I am targeting. I am not even confident I am finding the field properly above (probably not).

The fields in question are Managed Metadata fields that are part of a site column, which in turn is part of a content type applied to the document library.

EDIT: Complete code

$webApplicationURL = "http://www.site.com/"
$webApp = Get-SPWebApplication $webApplicationURL

if($webApp -ne $null)
{
  $webAppName = $webApp.name
  Write-Output "Web Application : $webAppName"

  foreach($siteColl in $webApp.Sites)
    { 
     if($siteColl -ne $null)
      {
        $siteCollName = $siteColl.Url
         Write-Output "Site Collection : $siteCollName"

         foreach($subWeb in $siteColl.AllWebs)
          {
            if($subWeb -ne $null)
              {
              $subWebName = $subWeb.Title
              Write-Output "Site : $subWebName"

              foreach ($list in $subWeb.Lists)
              {
                $listname = $list.Title
                if($lastlistname -ne $listname){Write-Output "List: $listname"}

                $FieldName = $list.Fields["NH Department"]
                Write-Output $("NH Department: " + $FieldName.DefaultValue)

              }
                 $subWeb.Dispose()
              }
           else
              {
                Echo $subWeb "does not exist"
              }
          }
            $siteColl.Dispose()
       }
     else
         {
            Echo $siteColl "does not exist"
         }
     }
} else
     {       Echo $webApplicationURL "does not exist, check the WebApplication name"
     }

1 Answer 1

1

There is property called DefaultValue.

$FieldName = $list.Fields["NH Department"]
Write-Output $("NH Department - Default Value " + $FieldName.DefaultValue)
4
  • yes, tried that as well... but it is not returning anything at all. Is it because it is a managed metadata term? foreach ($list in $subWeb.Lists) { $listname = $list.Title $FieldName = $list.Fields["NH Department"] Write-Output $("NH Department: " + $FieldName.DefaultValue) }
    – Kolten
    Jul 28, 2014 at 18:43
  • Does it have a default value? If it has a default value, it will return it. If not, it won't return anything--Regardless of if it's a managed metadata property.
    – Mike
    Jul 28, 2014 at 18:47
  • yes, for example, I navigated to a site's document library and say (via the GUI) that it has a default value. finding it in the output of the script though, it shows nothing Site : Board & Administrative Services List: Board & Admin Services Documents NH Department: I updated my OP with the complete code.
    – Kolten
    Jul 28, 2014 at 20:19
  • i figured it out. not sure why my code wasn't working, but made a simpler script which worked. thank you!
    – Kolten
    Jul 29, 2014 at 20:25

Your Answer

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

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