Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powershell Out-File: Force encoding to Ascii

I am attempting to normalize a set of TAB-delimited logfiles using Powershell.

Here is the current script:

(Get-ChildItem *.csv) |%{
#Store the name of the file & date
$Name = $_.basename
$FileDate = $_.CreationTime
#Prepends the following to each message: unique Identifer, Hostname, date
(Get-Content $_.fullname) -replace "^","AR-LOG|$Name|$FileDate|"|
#Replaces the TAB delimeter with a Pipe delimeter
Foreach-Object {$_ -replace '   ','|'}  |
#Appends the resulting message in ascii
Out-File -append -FilePath normalized.log -Encoding ascii

A snippet of the input & output can be seen here:

http://pastebin.com/uaQadYUC

How can I force the output file to be ascii and not some type of unicode?

***Edit: Further troubleshooting reveals that the input files are actually windows-1252 encoded, which apparently Get-Content cannot handle natively(?)

like image 389
Josh Brower Avatar asked Oct 18 '25 19:10

Josh Brower


1 Answers

You should be able to use the encoding flag on out-file as in ... | Out-File -encoding ascii myfile.txt. And if you're using append, make sure all appends use the same encoding or you'll end up with an unusable file.

like image 101
Bruce Payette Avatar answered Oct 20 '25 11:10

Bruce Payette



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!