Files created by Powershell are over 2X bigger than files created by text editor or cmd.exe.

1439 views
Skip to first unread message

tom.l...@gmail.com

unread,
May 16, 2008, 12:07:41 PM5/16/08
to
I'm trying to build telnet scripts using Powershell and run them with
the Telnet Script Tool (TST) but files created by Powershell are not
recognized as valid text/script files by TST.

I discovered files generated by Powershell are more than twice the
size of plain text files created outside of Powershell.

Here is the test:
[PowerShell]
"test"> test.a

[Cmd.exe]
echo test> test.b

A 'dir' of these files from a shell reveals:
05/16/2008 09:34 AM 14 test.a
05/16/2008 09:34 AM 6 test.b

*Notice the size of the files.

What's going on? The test.b file is appropriately 6 bytes: "test\r\n"
= 6 chars. Where are the extra 8 chars coming from in the PS-created
file?

The files in any text editor appear identical and Textpad said they
were identical as well.

Then I opened a PS-created file in MS Word 2007 and saw non-printable
chars before each char in the file.

Please help. Thx.

Karl Mitschke

unread,
May 16, 2008, 12:14:42 PM5/16/08
to
Hello tom,

It's not outputting in ASCII. (I beleive it's unicode)

Do this:

"test" |out-file -Encoding ascii test.c

Karl

tom.l...@gmail.com

unread,
May 16, 2008, 12:36:51 PM5/16/08
to
That did the trick! Thank you Karl.

I was starting to go down that path (unicode vs. ascii), but hadn't
nailed it yet.

Can I make ASCII the default output encoding? Is this wise?

Something that threw me off is $OutputEncoding. Why doesn't
$OutputEncoding show Unicode?

PS C:\> $OutputEncoding
IsSingleByte : True
BodyName : us-ascii
EncodingName : US-ASCII
HeaderName : us-ascii
WebName : us-ascii
WindowsCodePage : 1252
IsBrowserDisplay : False
IsBrowserSave : False
IsMailNewsDisplay : True
IsMailNewsSave : True
EncoderFallback : System.Text.EncoderReplacementFallback
DecoderFallback : System.Text.DecoderReplacementFallback
IsReadOnly : True
CodePage : 20127

Karl Mitschke

unread,
May 16, 2008, 3:13:03 PM5/16/08
to
Tom;

I have no idea, and i have no idea :)

alexandair

unread,
May 27, 2008, 6:06:13 AM5/27/08
to


There are two major ways to write files in PowerShell — with the Out-
File cmdlet and using the Set-Content cmdlet. Out-File will try to
format the output and text files are written in Unicode by default. We
can change that with -encoding parameter. Set-Content will simply
write the output and use ASCII encoding.

Also, when we pipe output data from PowerShell cmdlets into native
applications, the output encoding from PowerShell cmdlets is
controlled by the $OutputEncoding variable, which is by default set to
ASCII. You could find more details here:
http://blogs.msdn.com/powershell/archive/2006/12/11/outputencoding-to-the-rescue.aspx

-aleksandar
http://powershellers.blogspot.com

expr...@gmail.com

unread,
Oct 22, 2013, 9:43:41 PM10/22/13
to
Thank you Tom and Karl, 5 years later, spend a week with 6GB file that was doubling in size when we did some formatting with powershell. Took a week for us to find your post !!! But you fixed it !!

neil....@gmail.com

unread,
Sep 21, 2016, 10:39:16 PM9/21/16
to
8 years later and still paying dividends!

I've spent several hours trying to figure out why my truncated files were larger than their source... And then I stumbled on this post!

Thanks Karl and Tom!!

dhe...@gmail.com

unread,
Apr 8, 2020, 12:09:12 AM4/8/20
to
12 years later in 2020 checking in, THANK YOU FOR THIS ANSWER.

Ryan Tusia

unread,
Nov 5, 2021, 2:18:26 PMNov 5
to
Great friggin' answer. Simply amazing. Thank you!
Reply all
Reply to author
Forward
0 new messages