07 Nov 10 14 - Powershell script error

Tech_20
Tech_20 used Ask the Experts™
on
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!!!


Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Chris DentPowerShell Developer
Top Expert 2010

Commented:

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

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

Chris

Author

Commented:
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?
Chris DentPowerShell Developer
Top Expert 2010

Commented:

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
PMI ACP® Project Management

Prepare for the PMI Agile Certified Practitioner (PMI-ACP)® exam, which formally recognizes your knowledge of agile principles and your skill with agile techniques.

Author

Commented:
Both files are 4 KB each. Notepad file and PS generated file.
Chris DentPowerShell Developer
Top Expert 2010

Commented:

Hmm yet they still contain interesting characters?

Chris

Author

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

Author

Commented:
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!
Commented:
Final conclusion, 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 conclude it's a Powershell flaw. Thank for your attempt, Chris-Dent.

Author

Commented:
For anyone who does find a solution, please create an update.

Author

Commented:
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.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial