I want to export my SharePoint document library data on daily basis and csv file need to be save as naming convention as "Dataname_yyyyMMdd". As per my below script, it need to export data from the sharepoint library into one CSV file as name "Data_yyyyMMdd". Then it need to imports the data from "Data_yyyyMMdd" and adds "header" to the file and then export to new final output CSV as name "DataName_yyyyMMdd".
While running the script i am getting only one file as "Data_yyyyMMdd". The data from file was not importing to final csv file and its not generating the final output file "DataName_yyyyMMdd". Please correct me if doing wrong in script. Please share me correct script.
Powershell script
$web = get-spweb $siteUrl
$caseLib = $web.lists | where {$_.title -eq $listTitle}
$query=new-object Microsoft.SharePoint.SPQuery
$query.ViewFields = "<FieldRef Name='LinkFilename'/><FieldRef Name='DocumentSetDescription'/>"
$query.RowLimit=5000
do
$ListName1 = "Data"
$ExportFolder1 = “C:\Users\”
$ExportName1 = Get-Date -f “yyyyMMdd”
$ExportPath1 = $ExportFolder1 + $ListName1 + $ExportName1 + “.csv”
$ListName = "Dataname_"
$ExportFolder = “C:\Users\csv\”
$ExportName = Get-Date -f “yyyyMMdd”
$ExportPath = $ExportFolder + $ListName + $ExportName + “.csv”
{
$caseLibItems=$caseLib.GetItems($query)
$query.ListItemCollectionPosition=$caseLibItems.ListItemCollectionPosition
$listItemsTotal = $caseLibItems.Count
$x = 0
for($x=0;$x -lt $listItemsTotal; $x++)
{
$Description = $caseLibItems[$x]["DocumentSetDescription"]
$str = ""
if('$Description' -ne $null)
{
$Description = $Description -replace "`n"," " -replace "`r"," "
$str = $caseLibItems[$x]["LinkFilename"].ToString() + '}' + $Description
}
else
{
$str = $caseLibItems[$x]["LinkFilename"].ToString()
}
Write-Output $str | Out-File $ExportPath1 -Append
}
} while ($query.ListItemCollectionPosition -ne $null)
Import-csv $ExportPath1 -delimiter "}" -Header "Number", "Description" | export-csv $ExportPath -NoTypeInformation
Write-Host "Exiting"