Python253

default_powershell_properties

Apr 27th, 2024
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.22 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # Filename: default_powershell_properties.py
  4. # Version: 1.0.0
  5. # Author: Jeoi Reqi
  6.  
  7. """
  8. This script gathers the default PowerShell properties and outputs them in the terminal.
  9.  
  10. Requirements:
  11.    - Python 3.x
  12.    - Windows operating system
  13.  
  14. Usage:
  15.    Run the script in a Python environment. It will automatically retrieve the default properties of the PowerShell window and display them in the terminal.
  16.  
  17. Functions:
  18.    get_powershell_properties():
  19.        Retrieves the default properties of the PowerShell window.
  20.        Returns:
  21.            dict: A dictionary containing the default properties:
  22.                - "Window Size": Tuple of width and height of the PowerShell window.
  23.                - "Font Name": Default font name used in the PowerShell window (Consolas).
  24.                - "Font Size": Default font size used in the PowerShell window (12).
  25.  
  26. Example Output:
  27.    - 64-bit PowerShell Console Properties:
  28.    - ScreenBufferSize: 11806479
  29.    - WindowSize: 11796555
  30.    - FontSize: 786432
  31.    - FaceName: Consolas
  32.    - FontWeight: 400
  33.    - QuickEdit: 1
  34.    - ScreenColors: 2
  35.  
  36. Additional Notes:
  37.    - If the PowerShell window is not found or an error occurs during retrieval, an error message will be printed.
  38. """
  39.  
  40.  
  41. import winreg
  42.  
  43. def get_console_properties(key_path):
  44.     """
  45.    Retrieves the properties of the specified console from the registry.
  46.  
  47.    Parameters:
  48.        key_path (str): The registry key path.
  49.  
  50.    Returns:
  51.        dict: A dictionary containing the console properties if found, otherwise None.
  52.    """
  53.     console_properties = {}
  54.     try:
  55.         with winreg.OpenKey(winreg.HKEY_CURRENT_USER, key_path) as key:
  56.             # Iterate over the values in the registry key
  57.             for i in range(winreg.QueryInfoKey(key)[1]):
  58.                 name, value, _ = winreg.EnumValue(key, i)
  59.                 console_properties[name] = value
  60.         return console_properties
  61.     except FileNotFoundError:
  62.         return None
  63.  
  64. # Define the registry paths for both 32-bit and 64-bit PowerShell consoles
  65. powershell_32bit_key_path = r"HKEY_CURRENT_USER\Console\%SystemRoot%_SysWOW64_WindowsPowerShell_v1.0_powershell.exe"
  66. powershell_64bit_key_path = r"HKEY_CURRENT_USER\Console\%SystemRoot%_System32_WindowsPowerShell_v1.0_powershell.exe"
  67.  
  68. # Get properties for the 64-bit PowerShell console
  69. powershell_64bit_properties = get_console_properties(powershell_64bit_key_path)
  70.  
  71. # Get properties for the 32-bit PowerShell console if 64-bit not found
  72. if powershell_64bit_properties is None:
  73.     print("64-bit PowerShell Console not found. Checking for 32-bit PowerShell Console...")
  74.     powershell_32bit_properties = get_console_properties(powershell_32bit_key_path)
  75.  
  76.     # Display the properties for the 32-bit PowerShell console if found
  77.     if powershell_32bit_properties:
  78.         print("32-bit PowerShell Console Properties:")
  79.         for name, value in powershell_32bit_properties.items():
  80.             print(f"{name}: {value}")
  81.     else:
  82.         print("32-bit PowerShell Console not found.")
  83. else:
  84.     print("64-bit PowerShell Console Properties:")
  85.     for name, value in powershell_64bit_properties.items():
  86.         print(f"{name}: {value}")
  87.  
  88.  
Add Comment
Please, Sign In to add comment