Link to home
Start Free TrialLog in
Avatar of Tech_20
Tech_20

asked on

07 Nov 10 14 - Powershell script error

Powershell script error

When I use the “Add-Content” command it works but anything I append to a text file produces Asian characters. I tested the same script on a Windows 7 PC and it produces the same issue. Just FYI, I included the script below. I created a file to list BIOS info and then a following line to append important process information below the BIOS info. Again it appends but it’s in Asian code.
________________________________________________

Get-WmiObject win32_bios | out-file “C:\<filename>.txt”
$T = (Get-Process) | Sort-Object CPU –descending | select –first 10
Add-Content C:\new_pcinfo_bios.txt $T
________________________________________________


Heeeelp!!!


Avatar of Chris Dent
Chris Dent
Flag of United Kingdom of Great Britain and Northern Ireland image


It's probably a problem with encoding. Try this perhaps:

 ... | Out-File C:\<filename>.txt -Encoding ASCII

Chris
Avatar of Tech_20
Tech_20

ASKER

Thanks, unfortunately it didn’t work. You have the right idea. I tried the following code.
...| Out-File C:\<filename>.txt -Encoding ASCII

I also tried to update the Output Encoding with the following two attempts (same result, Asian characters).
PS> $OutputEncoding = [Text.Encoding]::ASCII
PS> $OutputEncoding = New-Object -typename System.Text.UTF8Encoding

This link suggests there is a flaw with Powershell. Below is the quote and link.

 “The biggest flaw in CMD is its dependency on legacy encodings, and that heritage seems to still be alive in PowerShell. Unless there is rational reason, this seems like a major flaw.”

http://blogs.msdn.com/b/powershell/archive/2006/12/11/outputencoding-to-the-rescue.aspx


…any other suggestions?

Can you make the file in PowerShell and make a file using Notepad with the same content and compare the sizes?

If it different we're very likely to be on the right track even if the work-around isn't working yet.

Chris
Avatar of Tech_20

ASKER

Both files are 4 KB each. Notepad file and PS generated file.

Hmm yet they still contain interesting characters?

Chris
Avatar of Tech_20

ASKER

So far, yes, Asian (I think Chinese) characters. Just tried the chcp command and it lists the correct code, 437 for United States.
Avatar of Tech_20

ASKER

After looking around, I am at the same conclusion. As I mentioned before, the link below suggests there is a flaw with Powershell. Below is the quote and link.

 “The biggest flaw in CMD is its dependency on legacy encodings, and that heritage seems to still be alive in PowerShell. Unless there is rational reason, this seems like a major flaw.”

http://blogs.msdn.com/b/powershell/archive/2006/12/11/outputencoding-to-the-rescue.aspx

At this point I'll plan to close the question if possible unless someone else has a solution. Thanks anyway!
ASKER CERTIFIED SOLUTION
Avatar of Tech_20
Tech_20

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Tech_20

ASKER

For anyone who does find a solution, please create an update.
Avatar of Tech_20

ASKER

After a thorough search, the current conclusion is that Powershell has flaws with some of its code as confirmed on several reputable blogs from other tech experts who have tried to use the same code. For anyone who does find a solution, please create an update.