0

I'm systematically renaming a bunch of files via PowerShell 2, and I want to output old and new filemappings to a tab-delimited output file in UTF-8. The problem is that Export-Csv (and other output methods) force a BOM to the beginning of that tsv file and breaks the output, stopping items from being properly tab-delimited and just jamming everything into the first column. I suppose if I could suppress the BOM this issue would be fixed, but any other recommendations are welcome. Code below.

Set-Location -Path ""
$destLoc = ""
$countRef = [ref] 0
Get-ChildItem -Filter *.pdf -Recurse | ForEach-Object {
  $newFullName = '{0}\{1}.pdf' -f $destLoc, ++$countRef.Value
  Copy-Item -LiteralPath $_.FullName -Destination $newFullName
  New-Object PSCustomObject -Property @{
    Old = $_.FullName
    New = $newFullName
  }
} | Export-Csv -Delimiter "`t" -Encoding UTF8 NameMappings.tsv
  • How to remove BOM from an UTF8 file in PowerShell is detailed here: https://stackoverflow.com/questions/5596982/using-powershell-to-write-a-file-in-utf-8-without-the-bom – Gzeh Niert Nov 08 '18 at 17:52

1 Answers1

0

Just for posterity sake, anyone coming into the same problem as me, don't let excel automatically handle the incoming tsvs text to columns import. The problem was that the BOM was messing with that automatic formatting, but if you manually open a tsv file and specify delimited, you get the desired behavior regardless of encoding.