Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- # -*- coding: utf-8 -*-
- # Filename: default_console_configurations.py
- # Version: 1.0.0
- # Author: Jeoi Reqi
- """
- This script configures properties of both Command Prompt and PowerShell consoles based on the user's privileges.
- For Command Prompt, it sets the window size, font, QuickEdit mode, and text color.
- For PowerShell, it sets similar properties.
- Requirements:
- - Python 3.x
- - Windows operating system
- Functions:
- is_admin():
- Checks if the script is running with administrative privileges.
- Returns:
- bool: True if the script is running as administrator, False otherwise.
- set_console_registry_value(key_path, value_name, value_data, value_type):
- Sets a registry value for a given key path.
- Parameters:
- key_path (str): The full path to the registry key.
- value_name (str): The name of the registry value.
- value_data (int): The data to set for the registry value.
- value_type (int): The type of the registry value.
- Returns:
- str: Message indicating whether the operation was successful or not.
- get_text_color():
- Determines the appropriate text color based on the user's privilege level.
- Returns:
- int: The color code for the text (0-15).
- Usage:
- Run the script in a Python environment. It automatically adjusts console properties based on the user's privilege level.
- Additional Notes:
- - For Command Prompt, green text is used for the administrator console, while white text is used for a standard terminal.
- - The script sets properties for both Command Prompt and PowerShell consoles.
- """
- import winreg
- import ctypes
- def is_admin():
- try:
- return ctypes.windll.shell32.IsUserAnAdmin()
- except:
- return False
- def set_console_registry_value(key_path, value_name, value_data, value_type):
- full_key_path = r'HKEY_CURRENT_USER\Console\{}'.format(key_path)
- try:
- # Open the existing key
- key = winreg.OpenKey(winreg.HKEY_CURRENT_USER, full_key_path, 0, winreg.KEY_WRITE)
- except FileNotFoundError:
- return f"\nRESULT: ---> ---> ---> FAILURE!\nKey Path does not exist:\n{full_key_path.strip()}"
- try:
- # Set the registry value
- winreg.SetValueEx(key, value_name, 0, value_type, value_data)
- return f"\nRESULT: ---> ---> ---> SUCCESS!\nKey Path:\n{full_key_path.strip()}\nName: {value_name.strip()}\nValue: {value_data}"
- except Exception as e:
- return f"\nRESULT: ---> ---> ---> FAILURE!\nKey Path:\n{full_key_path.strip()}\nName: {value_name.strip()}\nValue: {value_data}\nError: {e}"
- finally:
- # Close the registry key
- winreg.CloseKey(key)
- def get_text_color():
- if is_admin():
- # Green text for administrator console
- return 2
- else:
- # White text for standard terminal
- return 15
- # Set properties for Command Prompt
- print("\nSetting properties for Command Prompt...")
- cmd_reg_key = r'%SystemRoot%_system32_cmd.exe'
- cmd_width = 180
- cmd_window_height = 75
- cmd_buffer_height = 9999
- cmd_screen_buffer_size = cmd_buffer_height + cmd_width * 0x10000
- cmd_window_size = cmd_window_height + cmd_width * 0x10000
- print(set_console_registry_value(cmd_reg_key, "ScreenBufferSize", cmd_screen_buffer_size, winreg.REG_DWORD))
- print(set_console_registry_value(cmd_reg_key, "WindowSize", cmd_window_size, winreg.REG_DWORD))
- cmd_font_reg_key = r'%SystemRoot%_system32_cmd.exe'
- cmd_font_name = 'Consolas'
- cmd_font_size = 12 * 0x10000
- print(set_console_registry_value(cmd_font_reg_key, "FontSize", cmd_font_size, winreg.REG_DWORD))
- print(set_console_registry_value(cmd_font_reg_key, "FaceName", cmd_font_name, winreg.REG_SZ))
- print(set_console_registry_value(cmd_font_reg_key, "FontWeight", 400, winreg.REG_DWORD))
- cmd_quick_edit_reg_key = r'%SystemRoot%_system32_cmd.exe'
- print(set_console_registry_value(cmd_quick_edit_reg_key, "QuickEdit", 1, winreg.REG_DWORD))
- cmd_color_reg_key = r'%SystemRoot%_system32_cmd.exe'
- fg = get_text_color()
- bg = 0
- cmd_color = 16 * bg + fg
- print(set_console_registry_value(cmd_color_reg_key, "ScreenColors", cmd_color, winreg.REG_DWORD))
- # Set properties for PowerShell
- print("\nSetting properties for PowerShell...")
- ps_reg_key = r'%SystemRoot%_System32_WindowsPowerShell_v1.0_powershell.exe'
- ps_width = 180
- ps_window_height = 75
- ps_buffer_height = 9999
- ps_screen_buffer_size = ps_buffer_height + ps_width * 0x10000
- ps_window_size = ps_window_height + ps_width * 0x10000
- print(set_console_registry_value(ps_reg_key, "ScreenBufferSize", ps_screen_buffer_size, winreg.REG_DWORD))
- print(set_console_registry_value(ps_reg_key, "WindowSize", ps_window_size, winreg.REG_DWORD))
- ps_font_reg_key = r'%SystemRoot%_System32_WindowsPowerShell_v1.0_powershell.exe'
- ps_font_name = 'Consolas'
- ps_font_size = 12 * 0x10000
- print(set_console_registry_value(ps_font_reg_key, "FontSize", ps_font_size, winreg.REG_DWORD))
- print(set_console_registry_value(ps_font_reg_key, "FaceName", ps_font_name, winreg.REG_SZ))
- print(set_console_registry_value(ps_font_reg_key, "FontWeight", 400, winreg.REG_DWORD))
- ps_quick_edit_reg_key = r'%SystemRoot%_System32_WindowsPowerShell_v1.0_powershell.exe'
- print(set_console_registry_value(ps_quick_edit_reg_key, "QuickEdit", 1, winreg.REG_DWORD))
- ps_color_reg_key = r'%SystemRoot%_System32_WindowsPowerShell_v1.0_powershell.exe'
- fg = get_text_color()
- bg = 0
- ps_color = 16 * bg + fg
- print(set_console_registry_value(ps_color_reg_key, "ScreenColors", ps_color, winreg.REG_DWORD))
Add Comment
Please, Sign In to add comment