protected override void EndProcessing()
        {
            var stdOutHandle = NativeMethods.GetStdHandle(-11);

            CONSOLE_SCREEN_BUFFER_INFOEX screenBufferInfo = new CONSOLE_SCREEN_BUFFER_INFOEX
            {
                cbSize = Marshal.SizeOf <CONSOLE_SCREEN_BUFFER_INFOEX>()
            };

            if (!NativeMethods.GetConsoleScreenBufferInfoEx(stdOutHandle, ref screenBufferInfo))
            {
                WriteError(new ErrorRecord(
                               null,
                               "ErrorGettingScreenBufferInfo",
                               ErrorCategory.InvalidOperation,
                               "Error getting screen buffer info"));
                return;
            }

            if (ParameterSetName == "SingleColor")
            {
                SetColorHelper(ConsoleColor, RGB, ref screenBufferInfo);
            }
            else
            {
                var pairs = ColorTable.GetEnumerator();
                while (pairs.MoveNext())
                {
                    var consoleColor = pairs.Key as ConsoleColor? ?? LanguagePrimitives.ConvertTo <ConsoleColor>(pairs.Key);
                    var color        = pairs.Value as RGBColor? ?? LanguagePrimitives.ConvertTo <RGBColor>(pairs.Value);
                    SetColorHelper(consoleColor, color, ref screenBufferInfo);
                }
            }

            if (TextForegroundColor.HasValue)
            {
                screenBufferInfo.wAttributes = (short)((screenBufferInfo.wAttributes & 0xfff0) | (int)TextForegroundColor.Value);
            }

            if (TextBackgroundColor.HasValue)
            {
                screenBufferInfo.wAttributes = (short)((screenBufferInfo.wAttributes & 0xff0f) | (int)TextBackgroundColor.Value << 4);
            }

            if (PopupForegroundColor.HasValue)
            {
                screenBufferInfo.wPopupAttributes = (short)((screenBufferInfo.wPopupAttributes & 0xfff0) | (int)PopupForegroundColor.Value);
            }

            if (PopupBackgroundColor.HasValue)
            {
                screenBufferInfo.wPopupAttributes = (short)((screenBufferInfo.wPopupAttributes & 0xff0f) | (int)PopupBackgroundColor.Value << 4);
            }

            // conhost returns an off-by-one window size, so we must compensate
            screenBufferInfo.srWindow.Bottom += 1;
            screenBufferInfo.srWindow.Right  += 1;
            NativeMethods.SetConsoleScreenBufferInfoEx(stdOutHandle, ref screenBufferInfo);
        }
        protected override void EndProcessing()
        {
            var stdOutHandle = NativeMethods.GetStdHandle(-11);

            CONSOLE_SCREEN_BUFFER_INFOEX screenBufferInfo = new CONSOLE_SCREEN_BUFFER_INFOEX
            {
                cbSize = Marshal.SizeOf <CONSOLE_SCREEN_BUFFER_INFOEX>()
            };

            NativeMethods.GetConsoleScreenBufferInfoEx(stdOutHandle, ref screenBufferInfo);

            WriteObject(screenBufferInfo);
        }
        private void SetColorHelper(ConsoleColor consoleColor, RGBColor color, ref CONSOLE_SCREEN_BUFFER_INFOEX screenBufferInfo)
        {
            switch (consoleColor)
            {
            case ConsoleColor.Black:       screenBufferInfo.Black = color; break;

            case ConsoleColor.DarkBlue:    screenBufferInfo.DarkBlue = color; break;

            case ConsoleColor.DarkGreen:   screenBufferInfo.DarkGreen = color; break;

            case ConsoleColor.DarkCyan:    screenBufferInfo.DarkCyan = color; break;

            case ConsoleColor.DarkRed:     screenBufferInfo.DarkRed = color; break;

            case ConsoleColor.DarkMagenta: screenBufferInfo.DarkMagenta = color; break;

            case ConsoleColor.DarkYellow:  screenBufferInfo.DarkYellow = color; break;

            case ConsoleColor.Gray:        screenBufferInfo.Gray = color; break;

            case ConsoleColor.DarkGray:    screenBufferInfo.DarkGray = color; break;

            case ConsoleColor.Blue:        screenBufferInfo.Blue = color; break;

            case ConsoleColor.Green:       screenBufferInfo.Green = color; break;

            case ConsoleColor.Cyan:        screenBufferInfo.Cyan = color; break;

            case ConsoleColor.Red:         screenBufferInfo.Red = color; break;

            case ConsoleColor.Magenta:     screenBufferInfo.Magenta = color; break;

            case ConsoleColor.Yellow:      screenBufferInfo.Yellow = color; break;

            case ConsoleColor.White:       screenBufferInfo.White = color; break;
            }
        }
 public static extern bool GetConsoleScreenBufferInfoEx(
     IntPtr hConsoleOutput,
     ref CONSOLE_SCREEN_BUFFER_INFOEX lpConsoleScreenBufferInfoEx);