X
X
x2sp2019-07-08 18:10:43
SharePoint
x2sp, 2019-07-08 18:10:43

Why PS when exporting from Sharepoint incorrectly forms the first line?

Good afternoon.
We have the following result when uploading 4 values ​​from SP to CSV:

"RegData","podrazdelenieisp","ID","DataSozdani9"
"12/10/2018 12:00:00 AM","1;#????????","970","1/1/2018 12:00:00 AM"
"12/10/2018 12:00:00 AM","1;#Department","971","1/1/2018 12:00:00 AM"
...

The script itself:
Add-PSSnapin microsoft.sharepoint.powershell
$weburl = "http://#####"
$site=(Get-SPSite -Identity $weburl)
$web=$site.RootWeb 
$listName = "####"
$Department = 1
    $list = $web.Lists[$listName]
    $items = $list.Items      
       ForEach ($item in $items)    
       {                                    
            $a = $($item["Подразделение"])
            $a = $a -replace ";.*"
           
             If ($a -eq $Department)
                {
                
                  $Output =New-Object -TypeName PSObject -Property @{
                            ID = $item.Id
                            podrazdelenieisp = $item["Подразделение Исполнителя"]
                            RegData = $item["Дата регистрации"]
                            DataSozdani9 = $item["Дата создания"]                            
                         }
                           $Output | export-csv C:\organization.csv -Append
                         }
             }                                         
Remove-PSSnapIn Microsoft.SharePoint.PowerShell

I ran the following tests:
1) The variable with the first line inside PS is displayed correctly
2) When the csv upload location changes, the result does not change
3) I tried to exclude the first object (If ($a -eq $Department -and $item.id -ne 970), but I got the same error already with a different object on the first line
If the csv file is prepared in advance (create and mark columns), then there will be no error on the first line.But I'm worried about this behavior and I would like to find out the
reason.Thank you all.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
MaxKozlov, 2019-08-02
@MaxKozlov

It is necessary to take Export-Csv out of the loop, exclude -Append and add -Encoding

$output = foreach ($item in $items)    
{ 
    #...
    New-Object -TypeName PSObject -Property @{
        #...
    }
}
$output | Export-Csv -Encoding utf-8 -Path  C:\organization.csv

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question