question

PeterKiers-7182 avatar image
0 Votes"
PeterKiers-7182 asked MichaelHan-MSFT edited

Export a sharepoint list to excel with PowerShell

I have code in PowerShell that export a Sharepoint list to excel. Only I have a few Sharepoint Lists that has "/" sign in it. My code can not store a file that has a / in it. Is it possible to get a Sharepoint List with a / in it and change it to the minus "-" sign to save the file. Or just remove the / in the filename.

function ExportList($listName)
{
try
{
$listItems=(Get-PnPListItem -List $listName -Fields $Global:selectProperties).FieldValues
$outputFilePath="c:\Temp\" + $listName + ".xlsx"
$hashTable=@()
foreach($listItem in $listItems)
{
$obj=New-Object PSObject
$listItem.GetEnumerator() | Where-Object { $.Key -in $Global:selectProperties } |
ForEach-Object {
if( $
.Key -eq 'Datum' )
{
$obj | Add-Member Noteproperty $.Key $.Value.ToLocalTime().ToString("dd-MM-yyyy")
}
else
{
$obj | Add-Member Noteproperty $.Key $.Value
}
}
$hashTable+=$obj;
$obj=$null;
}

      $hashtable | Export-XLSX $outputFilePath -Table -Autofit -Force
   } 
   catch [Exception] 
   { 
      $ErrorMessage = $_.Exception.Message        
      Write-Host "Error: $ErrorMessage" -ForegroundColor Red         
   } 

}

ExportList("Dit is mijn lijst / map"); <==========

office-sharepoint-onlinewindows-server-powershellsharepoint-dev
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

RichMatheisen-8856 avatar image
0 Votes"
RichMatheisen-8856 answered

You only need to replace this line:

 $outputFilePath="c:\Temp\" + $listName + ".xlsx"

with this line:

 $outputFilePath="c:\Temp\" + ($listName -replace "/","-") + ".xlsx"

There are more characters that should be avoided in file and directory names than just a forward slash!

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.