Discussion:
Files created by Powershell are over 2X bigger than files created by text editor or cmd.exe.
(too old to reply)
t***@gmail.com
2008-05-16 16:07:41 UTC
Permalink
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
2008-05-16 16:14:42 UTC
Permalink
Hello tom,

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

Do this:

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

Karl
Post by t***@gmail.com
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.
[PowerShell]
"test"> test.a
[Cmd.exe]
echo test> test.b
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.
t***@gmail.com
2008-05-16 16:36:51 UTC
Permalink
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
2008-05-16 19:13:03 UTC
Permalink
Tom;

I have no idea, and i have no idea :)
Post by t***@gmail.com
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 : 2012
alexandair
2008-05-27 10:06:13 UTC
Permalink
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
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
e***@gmail.com
2013-10-23 01:43:41 UTC
Permalink
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 !!
n***@gmail.com
2016-09-22 02:39:11 UTC
Permalink
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!!
Post by Karl Mitschke
Hello tom,
It's not outputting in ASCII. (I beleive it's unicode)
"test" |out-file -Encoding ascii test.c
Karl
Post by t***@gmail.com
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.
[PowerShell]
"test"> test.a
[Cmd.exe]
echo test> test.b
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.
d***@gmail.com
2020-04-08 04:09:11 UTC
Permalink
Post by t***@gmail.com
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.
[PowerShell]
"test"> test.a
[Cmd.exe]
echo test> test.b
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.
12 years later in 2020 checking in, THANK YOU FOR THIS ANSWER.
Ryan Tusia
2021-11-05 18:18:24 UTC
Permalink
Great friggin' answer. Simply amazing. Thank you!

Continue reading on narkive:
Search results for 'Files created by Powershell are over 2X bigger than files created by text editor or cmd.exe.' (Questions and Answers)
64
replies
What's the difference between Windows and Linux?
started 2014-12-04 14:21:30 UTC
software
Loading...