queue
queue

Reputation: 33

Why am I getting gibberish when outputting to a text file from powershell?

I am trying to output all the file names in a directory to a file. Seems simple, but in the future I will be creating useful information based off the file names and outputting to a file for another system.

When I output the information to a file it shows as gibberish when I open in notepad. Outputting to the screen looks fine.

Here is my code:

$files = Get-ChildItem "s:\centmobile\rates\currentrates\forupload\"
$outfile = "s:\centmobile\rates\currentrates\test.txt" 
"New File"|Out-File $outfile -Encoding ascii

foreach ($f in $files){
Get-Content $f.FullName  | Add-Content $outfile -Encoding Ascii
Write-Output $f.FullName
}

Screen output looks good:

PS C:\Windows\System32\WindowsPowerShell\v1.0> S:\CentMobile\Software\Dev\cre8hdrinfo.ps1
S:\centmobile\rates\currentrates\forupload\2019406BICS_BC_Rates_ForUpload.xlsx
S:\centmobile\rates\currentrates\forupload\2019406BICS_FC_Rates_ForUpload.xlsx

File output looks not so good..

New File
PK     ! –~íGq  %   [Content_Types].xml ¢(                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  ¬”ËNÃ0E÷HüCä-Jܲ@5í‚Ç
A%ʘxÒXqlËã–öU(4BÍ&–ã™{O&3žÌ6NÖàQY“³q6b  ˜ÂJe–9û\¼¤÷,Á ŒÚÈÙͦ×W“ÅÖ&”m0gUîs,*hfÖ¡“ÒúFÚú%w¢¨ÅøíhtÇk˜†VƒM'OPŠ•Éó†^ïH<hdÉã.°õÊ™pN«B"åk#¹¤{‡Œ2cVÊá
a0ÞéОüm°Ï{§Òx%!™ÞDC|£ù·õõ—µuv^¤ƒÒ–¥*@ÚbÕP2t„Ä
 4:‹kÖeÜgüc0ò¸Œi¿/
÷p ý0o„~U¦F~ºšèT»*PÏË)¢L!†­º¢hŸs%<Èài\8Õ>ÇAÍ<÷Ö!µ‡ÿWá0·mvêH|PpœÜ®   8:Ò•pqÙÛÎ2d‡7—Üô  ÿÿ PK     ! µU0#ô   L   _rels/.rels ¢(                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  ¬’MOÃ0†ïHü‡È÷ÕÝBKwAH»!T~€Iܵ£$Ý¿'TƒG½~üÊÛÝ<êÈ!öâ4¬‹;#¶w­†—úqu*&r–Fq¬áÄvÕõÕö™GJy(v½*«¸¨¡KÉß#FÓñD±Ï.W   ¥†=™ZÆMYÞbø®ÕBSí­†°·7 ê“Ï›×–¦é
?ˆ9LìÒ™ÈsbgÙ®|Èl!õùUSh9i°bžr:"y_dlÀóD›¿ý|-NœÈR"4ø2ÏGÇ% õZ´4ñËyÄ7    ëÈðÉ‚‹¨Þ  ÿÿ PK     ! …ë —  †     xl/workbook.xml¬Umo›:þ~¥ýÆýLÁ¼ƒ’LI ÝJÛT¥]÷¥Òä€S¬æÓ¤ªößwlBš.ÕÔu‹ˆß?çœç&vu¥ÝÞQÖLutféirVÐævª¹ÊŒP×:›W¬!Sýtú‡Ù»&[ÆïÖŒÝi ÐtS½¢M³ËKRã¤•
ã50ä·f×r‚‹®$DÔ•i[–oÖ˜6ú€ó×`°Í†æ$ay_“F œTX ý®¤m7¢ÕùkàjÌïúÖÈYÝÄšVT<(P]«óøü¶a¯+0{‡<mÇáñá,hìñ&X:¹ª¦9gÛˆ3€6Ò'ö#ËDè™v§>x’krrOe¬¸ÿFVþËCÖ£!–ÒJÎ{#šwàfë³É†Väz®†Ûö3®e¤*]«p'Ò‚
RLõ †lKžMð¾]ô´‚U¹¶§›³ƒœ/¸V
...

Upvotes: 2

Views: 1620

Answers (2)

JosefZ
JosefZ

Reputation: 30103

Get-Content cmdlet returns strings or bytes (strings in your case). The gibberish you are getting comes from interpretation binary values from xlsx files as Ascii strings (unsolvable mojibake case).

Resources (required reading, incomplete):

From fileformatcommons.com:

xlsx files are actually zip files in disguise…
xlsx file character encoding / charset is binary

From .ZIP File Format Specification

local file header signature (4 bytes) 0x04034b50

From Zip (file format) at Wikipedia:

Most of the signatures end with the short integer 0x4b50, which is stored in little-endian ordering. Viewed as an ASCII string this reads "PK", the initials of the inventor Phil Katz. Thus, when a ZIP file is viewed in a text editor the first two bytes of the file are usually "PK".

Upvotes: 0

PMental
PMental

Reputation: 1179

The reason your screen output and file looks very different is that you're not outputting the same content at all to screen and file.

With:

Get-Content $f.FullName  | Add-Content $outfile -Encoding Ascii

you are, as the command implies, getting the content of every file and outputting to $outfile.

While with:

>Write-Output $f.FullName

You are just outputting the list of file names to screen.

As your question says it's the filenames you're after, just change:

Get-Content $f.FullName  | Add-Content $outfile -Encoding Ascii

to:

$f.FullName  | Add-Content $outfile -Encoding Ascii

and it should output the same thing to screen as to the file.

A good way to check/troubleshoot here would've been to just remove everything after:

Get-Content $f.FullName

and look at the output, which will look very similar to the file and give you a hint that something's wrong there.

Upvotes: 1

Related Questions