Question

When writing to a text file with .Net StreamWriter, Why does the text file take up less room on disk than when using PowerShell's Out-File. The following script creates the same text file with both StreamWriter and Out-File. But looking at the two text files produced, the 'StreamWriter' text file takes up half the room; why is this?

$String = 1..1000

# StreamWriter
$stream5 = [System.IO.StreamWriter] "E:\\Stream.txt"
$String | %{$stream5.WriteLine($_)}
$stream5.Close()

# Out-File
$string | Out-File "E:\Out-file.txt"
Was it helpful?

Solution

Out-File defaults to Unicode encoding. StreamWriter defaults to UTF8 which uses half the space for common western characters.

I guess the designers of Out-File made a mistake here and picked an unhelpful default. UTF8 is unicode just like UTF-16 is. Just smaller in many common cases. No functional difference.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top