# Convertto-CSV and out-file vs Export-Csv

**URL:** <https://forums.powershell.org/t/convertto-csv-and-out-file-vs-export-csv/11273>\
**Category:** PowerShell Help\
**Created:** [September 6, 2018, 3:10pm UTC](https://forums.powershell.org/t/convertto-csv-and-out-file-vs-export-csv/11273 "2018-09-06T15:10:43Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![ahart162](https://avatars.discourse-cdn.com/v4/letter/a/90db22/32.png) [@ahart162](https://forums.powershell.org/u/ahart162)\
**Post date:** [September 6, 2018, 3:10pm UTC](https://forums.powershell.org/t/convertto-csv-and-out-file-vs-export-csv/11273/1 "2018-09-06T15:10:43Z")

</div>

Hi,

Was just playing with exporting directory listings to CSV and noticed a little strangeness. I hope someone can enlighten me why this happens?

Two commands which seem to work the same,

[pre]Get-ChildItem | Select-Object fullname,length | ConvertTo-Csv |out-file -FilePath dir-list.csv[/pre]

[pre]Get-ChildItem | Select-Object fullname,length | Export-Csv -Path dir-list2.csv[/pre]

The resulting files look the same in Notepad++ and when 'cat’ed. But when the file sizes are checked the first command always creates a file about twice the size of the second command. I have opened both files in a hex editor and the larger file shows NULL (hex code 00) characters separating every character which accounts for the size difference. Why is this happening?

---

<div class="post-metadata">

**Author:** ![ta11ow](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/ta11ow/32/131_2.png) [@ta11ow](https://forums.powershell.org/u/ta11ow)\
**Post date:** [September 6, 2018, 3:26pm UTC](https://forums.powershell.org/t/convertto-csv-and-out-file-vs-export-csv/11273/2 "2018-09-06T15:26:43Z")

</div>

Ran a few tests myself, and I can say that it’s not the CSV cmdlets causing the difference. The issue appears to be the Out-File cmdlet, and it’s only present in Windows PowerShell 5.1, not PS Core (6.1.0 RC1). Unsure of prior versions, but it’s likely that it’s a long-standing bug that was fixed for PS Core at some point.

Instead, I’d suggest using the Set-Content or Add-Content cmdlet.

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [September 6, 2018, 3:27pm UTC](https://forums.powershell.org/t/convertto-csv-and-out-file-vs-export-csv/11273/3 "2018-09-06T15:27:19Z")

</div>

Use Notepad++ to check the encoding of the files. There you will see the difference. If you like to have it equally use this:

```
Get-ChildItem -exclude ‘dir-list*.csv’ | Select-Object fullname,length | ConvertTo-Csv -NoTypeInformation |out-file -FilePath dir-list.csv -Encoding utf8

Get-ChildItem -exclude ‘dir-list\*.csv’ | Select-Object fullname,length | Export-Csv -Path dir-list2.csv -Encoding utf8 -NoTypeInformation
```

---

<div class="post-metadata">

**Author:** ![ahart162](https://avatars.discourse-cdn.com/v4/letter/a/90db22/32.png) [@ahart162](https://forums.powershell.org/u/ahart162)\
**Post date:** [September 7, 2018, 4:44am UTC](https://forums.powershell.org/t/convertto-csv-and-out-file-vs-export-csv/11273/4 "2018-09-07T04:44:28Z")

</div>

I found out what is happening but not the why. I went back to double check the files in Notepad++ as @olaf-soyk suggested but couldn’t see any differences. I did notice that Notepad++ had decided that the files had difference encodings.  
The smaller file was UTF-8  
The larger file UCS-2 BE ROM

I haven’t come across UCS-2 BE ROM encoding before but a quick websearch showed it to be a 16-bit encoding as opposed to the UTF-8 which is 8-bit. I suppose it should have been obvious when I saw the extra empty chars in the hex editor!

Using out-file with -encoding utf8 gives files of equivalent size. There is still some BOM characters at the beginning of the file though. Hope this helps someone.

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [September 7, 2018, 5:39am UTC](https://forums.powershell.org/t/convertto-csv-and-out-file-vs-export-csv/11273/5 "2018-09-07T05:39:10Z")

</div>

BTW: That does not affect the functionality of the files. It takes a little more space and you could save some more “exotic” charachters from the unicode table. But it will work the same as UTF8 encoded files in common environments. 😉

---

<div class="post-metadata">

**Author:** ![js2010](https://avatars.discourse-cdn.com/v4/letter/j/ebca7d/32.png) [@js2010](https://forums.powershell.org/u/js2010)\
**Post date:** [September 7, 2018, 7:23am UTC](https://forums.powershell.org/t/convertto-csv-and-out-file-vs-export-csv/11273/6 "2018-09-07T07:23:07Z")

</div>

Yep. I had a thread about this a little while ago. At first I thought it was unix text. PS 5’s Out-File (or “\>”) encodes in what Notepad calls “unicode” and most other commands output in what Notepad calls “ansi”. Some applications won’t like it, like Infoblox (for csv import).

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:34pm UTC](https://forums.powershell.org/t/convertto-csv-and-out-file-vs-export-csv/11273/7 "2024-05-16T20:34:23Z")

</div>


