Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
How to enable in a PowerShell console
# To enable ANSI sequences in a PowerShell console run the following commands.
# After that you can use in you PowerShell just lake that:
# (curl -UserAgent "curl" ).Content
# More on it:
# (jp)
Add-Type -MemberDefinition @"
[DllImport("kernel32.dll", SetLastError=true)]
public static extern bool SetConsoleMode(IntPtr hConsoleHandle, int mode);
[DllImport("kernel32.dll", SetLastError=true)]
public static extern IntPtr GetStdHandle(int handle);
[DllImport("kernel32.dll", SetLastError=true)]
public static extern bool GetConsoleMode(IntPtr handle, out int mode);
"@ -Namespace Win32 -Name NativeMethods
# コンソールモードを変更
$Handle = [Win32.NativeMethods]::GetStdHandle(-11) # stdout
$Mode = 0
$Result = [Win32.NativeMethods]::GetConsoleMode($Handle, [ref]$Mode)
$Mode = $Mode -bor 4 # undocumented flag to enable ansi/vt100
$Result = [Win32.NativeMethods]::SetConsoleMode($Handle, $Mode)
chcp 437

This comment has been minimized.

Copy link

@strangeronline strangeronline commented Nov 21, 2019

I run without
PS C:\WINDOWS\system32>$PSVersionTable

Name Value

PSVersion 5.1.18362.145
PSEdition Desktop
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
BuildVersion 10.0.18362.145
CLRVersion 4.0.30319.42000
WSManStackVersion 3.0
PSRemotingProtocolVersion 2.3

PS C:\WINDOWS\system32>(curl -UserAgent "curl" -UseBasicParsing).Content
Weather report: Zaporizhia, Ukraine
Follow @igor_chubin for updates


This comment has been minimized.

Copy link
Owner Author

@chubin chubin commented Nov 21, 2019

@strangeronline Yes, the gist is already 4 years old. Now it should just work out of the box, as you did it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment