2

I want to export data from document library column into csv file using PowerShell.

In document library i had column name "Post Code" under column i had different post codes as "XXXXX-12-AD-2345, XXXXX-24-AD-2134, XXXXX-12-CD-1111, AAAAA-34-CR-1232, CCCCC-90-CX-11222, etc". I want data of "Post code" column and "post code" starting with XXXXX- only with all columns.

Means output like be in CSV file as below

enter image description here

Output on csv file as below

Post Code    Name   Contact Name    Phone number

XXXXX-02-AD-1234    Jhonson Jhonson@gmail.com   12345678

XXXXX-24-AD-2134    Mike    Mike@gmail.com  34534564

XXXXX-12-CD-1111    Peter   Peter@gmail.com     11222233

I tried "Where-OPostcoder getting data of "Post code" column, but it generating empty file. Is there other condition (or) other powershell script using Loop condtion etc. Please share script. Thank you so much in Advance.

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue


#Get the Web
$web = Get-SPWeb -identity "site url"

#Get the Target List
$list = $web.Lists["Document Library"]

#Array to Hold Result - PSObjects
$ListItemCollection = @()

 #Get All List items
 $list.Items | Where-Object {$_.'Post code' -like '*XXXXX-*'} | foreach {
$ExportItem = New-Object PSObject

$ExportItem | Add-Member -MemberType NoteProperty -name "Post code" -value $_["Post code"]

$ExportItem | Add-Member -MemberType NoteProperty -name "Name" -value $_["Name"]

$ExportItem | Add-Member -MemberType NoteProperty -name "Contact name" -value $_["Contact Name"]

$ExportItem | Add-Member -MemberType NoteProperty -name "Phone number" -value $_["Phone number"]

 #Add the object with property to an Array
 $ListItemCollection += $ExportItem
 }
 #Export the result Array to CSV file
 $ListItemCollection | Export-CSV "Path location" -NoTypeInformation                       

#Dispose the web Object
$web.Dispose()

write-Host "Exiting"
2

Modify code below

$_.'Post code'

to

$_["Post code"]

Full code:

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

#Get the Web
$web = Get-SPWeb -identity "site url"

#Get the Target List
$list = $web.Lists["Document Library"]

#Array to Hold Result - PSObjects
$ListItemCollection = @()

#Get All List items
$list.Items | Where-Object {$_["Post code"] -like '*XXXXX-*'} | foreach {
    $ExportItem = New-Object PSObject
    $ExportItem | Add-Member -MemberType NoteProperty -name "Post code" -value $_["Post code"]
    $ExportItem | Add-Member -MemberType NoteProperty -name "Name" -value $_["Name"]
    $ExportItem | Add-Member -MemberType NoteProperty -name "Contact name" -value $_["Contact Name"]
    $ExportItem | Add-Member -MemberType NoteProperty -name "Phone number" -value $_["Phone number"]
    #Add the object with property to an Array
    $ListItemCollection += $ExportItem
}
#Export the result Array to CSV file
$ListItemCollection | Export-CSV "Path location" -NoTypeInformation                       

#Dispose the web Object
$web.Dispose()

write-Host "Exiting"
1

At first glance, the following line is incorrect

$list.Items |  Get-Command | Where-Object ...

The Get-Command is incorrect and you are passing its output to Where-Object instead of $list.Items

Try this instead.

 $list.Items | Where-Object ...

Also i noticed that your spelling of Post Code in the Where-Object should be a capital c for Code

  • i tried removing Get-Command also, i am getting empty file only. Please refer updated code above. Please suggest other option – murali kuruva Sep 16 '18 at 7:14

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.