Because of the language, I want to use chcp 65001 to let the command window become English, and call systeminfo to get data.
However, using Windows 7 after using chcp 65001 the program systeminfo can't find anything but instead shows the error:
OS Name: A device attached to the system is not functioning
Is chcp 65001 not supported on Windows 7?
Yes: chcp.com exists in Windows 7. On both x32 and x64.
Yes: it supports 65001 as parameter to set UTF-8 as codepage.
However, this will rarily help you, as many things are not supported with this codepage in a console. See How to use unicode characters in Windows command line?
Related
Why does the tree command look different on Powershell than what it does on Cmd.
The problem only occurs in the PowerShell ISE, where output from external programs such as tree.com isn't passed straight through to the console.
Consider migrating away from the PowerShell ISE, given that it is no longer actively developed and there are reasons not to use it (bottom section), notably the inability to run PowerShell [Core] 6+, where all future effort will go. The editor that offers the best PowerShell development experience, across platforms, is Visual Studio Code, combined with its PowerShell extension, which is under active development.
To fix it, you must (temporarily) change [Console]::OutputEncoding to match your system's active OEM legacy code page, because that is the character encoding tree.com uses:
# Switch the encoding that PowerShell expects external programs to use
# to the active OEM code page.
[Console]::OutputEncoding = [System.Text.Encoding]::GetEncoding(
[cultureinfo]::CurrentCulture.TextInfo.OEMCodePage
)
# tree.com now produces the expected output in the ISE
tree
Note:
Even in a regular console window / Windows Terminal window and in Visual Studio Code [Console]::OutputEncoding can come into play, but only when you capture or redirect an external program's output (e.g., $treeOutput = tree or tree | ...).
As indicated above, this is not necessary for direct-to-display output - except in the ISE, where [Console]::OutputEncoding matters even then, which default to the system's active ANSI legacy code page.
For PowerShell to correctly interpret an external program's output when capturing or redirecting it, [Console]::OutputEncoding must match the actual encoding used by that program.
Therefore, if you want to capture or redirect tree.coms output, you may have to set [Console]::OutputEncoding:
In short, unless [Console]::OutputEncoding.CodePage matches the code-page number reported by [cultureinfo]::CurrentCulture.TextInfo.OEMCodePage - e.g., 437 on US-English systems - setting [Console]::OutputEncoding is needed.
In console windows and Windows Terminal windows this is generally not necessary (as of PowerShell 7.0) - it is only necessary if you've explicitly changed the active code page to UTF-8, for instance - see this answer; note that UTF-8 may become the default over time.
In Visual Studio Code, however, it is necessary by default, because in the PowerShell Integrated Console [Console]::OutputEncoding defaults to UTF-8 (code page 65001).
I believe it's because it's one of those commands that PowerShell runs cmd for in the background then pipes the output to Write-Host, similar to ping or ipconfig
Let's have a look at this:
✓ Hello, 世界
As you can see there is a unicode checkmark and chinese/japanese characters. In go, If I use MSYS or linux environment i can easily print those characters. Even on windows. However, I am unable to see them in CMD nor in Powershell.
I got this:
This is my very basic code:
package main
import (
"fmt"
)
func main() {
fmt.Println("✓ Hello, 世界")
// OR
fmt.Println("\u2713 Hello, 世界")
}
Also, I have dozens of console apps and they are able to show such characters on my windows using cmd or powershell. Why go can't?
The problem for the Windows cmd and PowerShell consoles is the lack of CJK characters in fonts such as Consolas and Lucida Console. On Windows 10, change the console font to a font that supports CJK characters, for example, NSimSun or SimSun-ExtB.
For example,
font.go:
package main
import (
"fmt"
)
func main() {
fmt.Println("✓ Hello, 世界")
// OR
fmt.Println("\u2713 Hello, 世界")
}
Output (NSimSun):
Microsoft Windows [Version 10.0.17134.345]
>go version
go version devel +47cc59f31f Tue Oct 23 00:29:57 2018 +0000 windows/amd64
>go run font.go
✓ Hello, 世界
✓ Hello, 世界
>
Try running in Windows PowerShell ISE.
It has fairly good support for displaying Unicode.
CMD and PowerShell don't support Unicode fonts in the command line shell very well because they aren't really using "fonts" to display the text, but ANSI/VT code pages that translate to characters. So despite PowerShell being able to support Unicode internally, and you can pass them between objects and functions, the command line shell simply cannot display them, as the translation between code page and displaying doesn't exist. Some attempts to make Unicode a little better can be found here:
Displaying Unicode in Powershell
For an interesting read on why CMD and PowerShell can't do Unicode well, see the blog post series: Windows Command-Line: Inside the Windows Console
Windows PowerShell ISE is different than running PowerShell from the command line shell as the ISE is displaying the output and doesn't have to use ANSI/VT sequences to pass commands between the shell and the PowerShell console host. And it doesn't have any legacy scripts to deal with.
Use Windows Terminal. You can get it on Microsoft Store.
Using PostgreSQL when I connect to a db using \c testdb inside PostgreSQL Database SQL Prompt. I successfully connect to the db but getting the following warning:
postgres-# \c testdb
WARNING: Console code page (437) differs from Windows code page (1252)
8-bit characters might not work correctly. See psql reference
page "Notes for Windows users" for details.
You are now connected to database "testdb" as user "postgres".
testdb-#
What does this warning mean? How to resolve it?
From the psql documentation:
psql is built as a "console application". Since the Windows console
windows use a different encoding than the rest of the system, you must
take special care when using 8-bit characters within psql. If psql
detects a problematic console code page, it will warn you at startup.
To change the console code page, two things are necessary:
Set the code page by entering cmd.exe /c chcp 1252. (1252 is a code
page that is appropriate for German; replace it with your value.) If
you are using Cygwin, you can put this command in /etc/profile.
So to remove that warning you need to execute chcp 1252 before you enterpsql. Using chcp without parameters gives you the current codepage.
The default codepage for CMD.exe is different than the default for postgres... To change for CMD.exe using the REGISTRY try this:
Start -> Run -> regedit
Go to [HKEY_LOCAL_MACHINE\Software\Microsoft\Command Processor]
Add new string value named "Autorun" with value "chcp 1252"
Then reopen CMD.exe
To make it even more obvious, the file to which #user3423801 is adding the line
cmd.exe /c chcp 1252
is in the scripts directory where you installed Postgre.
For example, in my case it is
C:\Program Files\PostgreSQL\9.3\scripts\runpsql.bat
Open cmd.exe and run regedit.
Go to Computer\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Command Processor
New a string value named: Autorun and change the value to be chcp 1252
Done.
Reference: https://stackoverflow.com/a/30100565/8396969
Please don't assume that Unix fixes work for Windows Users. For Windows 10, and PostgreSQL 12, combining the answers by "user3423801" and "numbers longer" worked for me. (The Windows Registry hack would not work. I did not try rebooting yet.) It is better to fix it in the PSQL startup script anyway.
The file location C:\Program Files\PostgreSQL\12\scripts contains the file runpsql.bat, into which you must insert the cmd.exe /c chcp 1252 command in the right location. So the top of your edited file should look like the 5 or 6 lines below.
#echo off
REM Copyright (c) 2012-2014, EnterpriseDB Corporation. All rights reserved
REM PostgreSQL server psql runner script for Windows
cmd.exe /c chcp 1252
SET server=localhost
SET /P server="Server [%server%]: "
The answer of dvdgsng is correct but with code example is more obviously.
#echo off
REM Copyright (c) 2012-2014, EnterpriseDB Corporation. All rights reserved
REM PostgreSQL server psql runner script for Windows
cmd.exe /c chcp 1252
SET server=localhost
SET /P server="Server [%server%]: "
Or you can simply type cmd.exe /c chcp 1252 in the Command Prompt window.
you just go to the power-shell or cmd.exe and type the command chcp 1252 or whatever the page number it wants "the one that is the windows code page". If the problem still persists, just open the console properties 'By clicking the power shell icon on the top left of the console window and choosing properties from the drop-down menu' and change the font to "Lucida Console". It worked for me, But you have to open power-shell as an Administrator.
The answers above are okay, but don't mention anywhere that Windows 1252 encoding is good for English language versions of Windows AND the other Western European Languages. This completes the answer for those people who may get confused about the aforementioned application to German language encoding. Yes it works for English without umlauts and other special characters needed for German, Spanish, French, Italian, Romanian, Hungarian, etc.
https://en.m.wikipedia.org/wiki/Windows-1252
What is Windows 1252 encoding?
Windows-1252 or CP-1252 (code page 1252) is a single-byte character encoding of the Latin alphabet, used by default in the legacy components of Microsoft Windows for English and some other Western languages (other languages use different default encodings).
After much digging for an answer that made sense to me, I found this help email chain at the PostgreSQL site which basically says to run chcp 1252 from inside an open command window.
I was then able to run my PostgreSQL commands without the code warning.
NOTE: this change does not persist so you have to run it every time you open a new command window where you plan to use PostgreSQL commands.
For Postgres 11
"WARNING: Console code page (437) differs from Windows code page
(1252)
8-bit characters might not work correctly. See psql reference
page "Notes for Windows users" for details."
If you aren't an administrator on your machine
Add a line "chcp 1252" to the pg_env.bat script found in the base directory of your postgres installation.'
i.e. "C:\Program Files\PostgreSQL\11"
If you are and Administrator on your machine
you can modify the registry to run the line everytime you run "cmd.exe" as mentioned above.
I couldn't figure out how to set it for Cygwin globally. This seemed to work though in my bash script
#!/bin/bash
cmd.exe /c chcp 1252 && psql -h myserver.postgres.database.azure.com -U myuser#prod-au -d mydatabase
Basically, set your console application encoding from 8-bit to utf-8 Windows 1252.
For git bash users
run the command chcp.com 1252 before running postgres
chcp is a windows console command, so to execute it on git bash you might need to add .com
git bash can't extend chcp to a full executable on its own, so you need to type the full command.
here
On the terminal screen go to the following directory;
C:\Program Files\PostgreSQL\14\bin
note: whichever database version you are using, go to the bin folder of the db version file.
Before creating a user or accessing the database user, you must write the following code;
cmd.exe /c chcp 1252
wmic process get /format:csv
What is wrong with this command in Windows 7?
I get:
Invalid XSL format (or) file name.
It's a bug in the wmic command. It's been suggested to copy XSL files into the %WINDIR%\system32 (or equivalent if 64 bit.) However, the command below works just as well without the need to copy files:
wmic process get ProcessId,Description,CommandLine,ExecutablePath,ParentProcessId /format:"%WINDIR%\System32\wbem\en-us\csv"
Of course, the "en-us" will be different for different locales.
If you want to redirect output to a file add a switch /output:"your filename here" before the process keyword
This is a bug in Windows 7 and Windows Server 2008 (also R2) WMIC. When you use (for example) Dutch regional settings in an English Windows installation, WMIC searches for the xsl files inside C:\Windows\System32\wbem\nl-NL, instead of C:\Windows\System32\wbem\en-US where they are.
Workarounds:
Copy or move the C:\Windows\system32\wbem\en-US\*.xsl files up into the C:\Windows\system32\wbem\ folder.
Change your regional settings to match your Windows language version, log out and back in.
Specify the full path: WMIC process get /format:"%WINDIR%\System32\wbem\en-US\csv".
This is a Windows 7 wmic bug. Change the International setings to English (United States), reboot and test.
I have yet another subtle problem on Windows :(
The following one-line perl script doesn't work:
perl -e "system('regedit.exe /s C:\my.reg');"
It really runs regedit.exe tool (I'm sure since I tried to run it w/o "/s" and saw confirmation dialogs), but it doesn't create a key in the registry.
I tried to run
regedit.exe /s C:\my.reg
in from windows shell (cmd.exe) and it works fine.
The original command works fine on Windows XP, but doesn't work on 2008 server.
So I suspect that this is system-related issue.
Are you executing this with an elevated cmd prompt (i.e. admin privileges)? Regedit requires this.