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!!!
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!!!
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?
...| 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
ASKER
Both files are 4 KB each. Notepad file and PS generated file.
Hmm yet they still contain interesting characters?
Chris
ASKER
So far, yes, Asian (I think Chinese) characters. Just tried the chcp command and it lists the correct code, 437 for United States.
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!
“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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
For anyone who does find a solution, please create an update.
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.
It's probably a problem with encoding. Try this perhaps:
... | Out-File C:\<filename>.txt -Encoding ASCII
Chris