I'm working on a Sharepoint 2010 site. I am trying to run this script from a website in order to create an xml containing info on my site columns.

$sourceWeb = Get-SPWeb http://farmstuff:1284
$xmlFilePath = "C:\Users\User\Scripts\Script-SiteColumns.xml"

#Create Export Files
New-Item $xmlFilePath -type file -force

#Export Site Columns to XML file
Add-Content $xmlFilePath "<?xml version=`"1.0`" encoding=`"utf-8`"?>"
Add-Content $xmlFilePath "`n<Fields>"
$sourceWeb.Fields | ForEach-Object {
   if ($_.Group -eq "Custom Columns") {
        Add-Content $xmlFilePath $_.SchemaXml
Add-Content $xmlFilePath "</Fields>"


And I get the following error:

An error occurred while enumerating through a collection: Exception has been thrown by the target of an invocation..
At C:\Users\User\Scripts\get-sitecolumns.ps1:10 char:5
+     <<<< %sourceWeb.Fields | ForEach-Object {
    + CategoryInfo           : InvalidOperation: (Microsoft.Share...on+SPEnumerator:SPEnumerator) [], Runtime Exception
    + FullyQualifiedErrorId: BadEnumeration

Any help would be appreciated! I am not too familiar with Sharepoint or Powershell nuances yet. Thanks.


<Field ID="{3C0E9E00-8FCC-479f-9D8D-3447CDA34C5B}" Name="OtherAddressCountry" StaticName="OtherAddressCountry" SourceID="http://schemas.microsoft.com/sharepoint/v3" DisplayName="Other Address Country" Group="Core Contact and Calendar Columns" Type="Text" Sealed="TRUE" AllowDeletion="TRUE" />
<Field ID="{9EBCD900-9D05-46c8-8F4D-E46E87328844}" Name="Categories" StaticName="Categories" SourceID="http://schemas.microsoft.com/sharepoint/v3" DisplayName="Categories" Group="Base Columns" Type="Text" Sealed="TRUE" AllowDeletion="TRUE" />
  • Does $web.Fields | % { $_.SchemaXml } alone works? In fact, I'm suspecting you have a unknown file type. This line should also fail in this case.
    – Steve B
    Commented Jul 19, 2012 at 14:43
  • 1
    If your code is copied from some site then it may have hidden characters or the apostrope near "`n<Fields>" may need to be deleted and reintroduced. I had similar situation where I had to go through line by line and fix hidden or messed up characters. I used Notepad++.
    – Bill Baer
    Commented Jul 19, 2012 at 14:46
  • @SteveB, I'm editing my main post with what's output when using your line (and replacing $web with $sourceWeb). It looks like it works twice and then chokes on something? I checked the original XML file and it has the same data in it as well.
    – John Smith
    Commented Jul 19, 2012 at 15:10
  • I hope this will works, try this : $sourceWeb.Fields[2] or $sourceWeb.Fields[2].ID if the former fail. If you are lucky, you will find the ID of the Field that is causing the issue. Starting with this ID, you will be able to search the faulty field. Probably a custom feature. I suggest you to use Notepad++ and do a search within all files of the 14 hives of kind xml, with the found ID as pattern. If I'm right, you'll find a <Field ID="xxx" />. Please post its declaration then.
    – Steve B
    Commented Jul 19, 2012 at 15:16
  • Hmmm, if I use [0], [5], [29], etc I have expected (good) output. But when I use [2] it has no output.
    – John Smith
    Commented Jul 19, 2012 at 15:29

1 Answer 1


Are you flexible and try the following method

foreach ($field in $web.Fields)
  if($field.Group -eq "Custom Column Group")
  • 1
    I get the following. C:\Users\User\Scripts\get-sitecolumns.ps1 : Exception has been thrown by the target of an invocation. At line:1 char2: + & <<<< .\get-sitecolumns.ps1 + CategoryInfo : NotSpecified: (:) [get-sitecolumns.ps1], TargetInvocationException + FullyQualifiedErrorId: System.Reflection.TargetInvocationException,get-sitecolumns.ps1 By the way I'm running this from the console with &
    – John Smith
    Commented Jul 19, 2012 at 15:19

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.