none
<<How to export a list using powershell?>> RRS feed

  • Question

  • Hi,

    Add-PSSnapin "Microsoft.SharePoint.PowerShell"
    
    $MyWeb = Get-SPWeb "http://webapp/site"
     $MyList = $MyWeb.Lists["Test"]
     $exportlist = @()
     $Mylist.Items | foreach {
     $obj = New-Object PSObject -Property @{
                 “Name” = $_["Title"]
                "Column 1" = $_["Column 1"]
                "Total" = $_["Total"] 
                "Grand Total" = $_["Grand Total"]                   
     }
     $exportlist += $obj 
     $exportlist | Export-Csv -path 'C:\Users\username\Desktop\export.csv'
     } 

    How can I display the columns as

    Title, Column 1, Total and Grand Total

    in the export file?

    Thanks.

    Thursday, April 6, 2017 3:28 PM

Answers

All replies

  • you just need to change the name in the created object to have the correct header in the CSV - something like this should work

    Add-PSSnapin "Microsoft.SharePoint.PowerShell"
    
    $MyWeb = Get-SPWeb "http://webapp/site"
    $MyList = $MyWeb.Lists["Test"]
    $Mylist.Items | foreach {
        New-Object PSObject -Property @{
            “Title” = $_["Title"]
            "Column 1" = $_["Column 1"]
            "Total" = $_["Total"] 
            "Grand Total" = $_["Grand Total"]                   
        }
     } | Export-Csv -path 'C:\Users\username\Desktop\export.csv' -NoTypeInformation

    Thursday, April 6, 2017 3:39 PM
  • Thanks. 

    I am getting this:

    • Grand Total
    • Title
    • Column 1
    • Total

    I need:

    • Title
    • Column 1
    • Total
    • Grand Total

    Can you please let me know how?

    Thursday, April 6, 2017 7:00 PM
  • depending on the PowerShell version you are running you can either:

    Change:  New-Object PSObject -Property @{
    to: [pscustomobject][ordered]@{

    or if that doesn't work (needs powershell 4 or better I believe)

    Change:  } | Export-Csv -path 'C:\Users\username\Desktop\export.csv' -NoTypeInformation

    to: Change:  }| select title, 'column 1', total, 'grand total' | Export-Csv -path 'C:\Users\username\Desktop\export.csv' -NoTypeInformation

    • Marked as answer by Andy__G Thursday, April 6, 2017 8:30 PM
    Thursday, April 6, 2017 7:50 PM
  • try this

    $MyWeb = WebUrl 
    $MyList = MyWeb.Lists["ListName"] 
    $MyItems = $MyList.Items 
    $MyItems | Select Fields.. | Export-Csv
    FilePath
    $MyWeb.Dispose()

    Sources:


    Thanks, Amit Kumar, LinkedIn Profile ** My Blog

    • Marked as answer by Andy__G Wednesday, May 10, 2017 4:40 PM
    Friday, April 7, 2017 6:29 AM