Advertisement
Guest User

Set-ScreenResolution

a guest
Aug 26th, 2021
10,066
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Sets the resolution of the main monitor- Requires PowerShell V2
  2.  
  3. PowerShell
  4. Function Set-ScreenResolution {
  5.  
  6. <#
  7.     .Synopsis
  8.         Sets the Screen Resolution of the primary monitor
  9.     .Description
  10.         Uses Pinvoke and ChangeDisplaySettings Win32API to make the change
  11.     .Example
  12.         Set-ScreenResolution -Width 1024 -Height 768        
  13.     #>
  14. param (
  15. [Parameter(Mandatory=$true,
  16.            Position = 0)]
  17. [int]
  18. $Width,
  19.  
  20. [Parameter(Mandatory=$true,
  21.            Position = 1)]
  22. [int]
  23. $Height
  24. )
  25.  
  26. $pinvokeCode = @"
  27.  
  28. using System;
  29. using System.Runtime.InteropServices;
  30.  
  31. namespace Resolution
  32. {
  33.  
  34.    [StructLayout(LayoutKind.Sequential)]
  35.    public struct DEVMODE1
  36.    {
  37.        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
  38.        public string dmDeviceName;
  39.        public short dmSpecVersion;
  40.        public short dmDriverVersion;
  41.        public short dmSize;
  42.        public short dmDriverExtra;
  43.        public int dmFields;
  44.  
  45.        public short dmOrientation;
  46.        public short dmPaperSize;
  47.        public short dmPaperLength;
  48.        public short dmPaperWidth;
  49.  
  50.        public short dmScale;
  51.        public short dmCopies;
  52.        public short dmDefaultSource;
  53.        public short dmPrintQuality;
  54.        public short dmColor;
  55.        public short dmDuplex;
  56.        public short dmYResolution;
  57.        public short dmTTOption;
  58.        public short dmCollate;
  59.        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
  60.        public string dmFormName;
  61.        public short dmLogPixels;
  62.        public short dmBitsPerPel;
  63.        public int dmPelsWidth;
  64.        public int dmPelsHeight;
  65.  
  66.        public int dmDisplayFlags;
  67.        public int dmDisplayFrequency;
  68.  
  69.        public int dmICMMethod;
  70.        public int dmICMIntent;
  71.        public int dmMediaType;
  72.        public int dmDitherType;
  73.        public int dmReserved1;
  74.        public int dmReserved2;
  75.  
  76.        public int dmPanningWidth;
  77.        public int dmPanningHeight;
  78.    };
  79.  
  80.  
  81.  
  82.    class User_32
  83.    {
  84.        [DllImport("user32.dll")]
  85.        public static extern int EnumDisplaySettings(string deviceName, int modeNum, ref DEVMODE1 devMode);
  86.        [DllImport("user32.dll")]
  87.        public static extern int ChangeDisplaySettings(ref DEVMODE1 devMode, int flags);
  88.  
  89.        public const int ENUM_CURRENT_SETTINGS = -1;
  90.        public const int CDS_UPDATEREGISTRY = 0x01;
  91.        public const int CDS_TEST = 0x02;
  92.        public const int DISP_CHANGE_SUCCESSFUL = 0;
  93.        public const int DISP_CHANGE_RESTART = 1;
  94.        public const int DISP_CHANGE_FAILED = -1;
  95.    }
  96.  
  97.  
  98.  
  99.    public class PrmaryScreenResolution
  100.    {
  101.        static public string ChangeResolution(int width, int height)
  102.        {
  103.  
  104.            DEVMODE1 dm = GetDevMode1();
  105.  
  106.            if (0 != User_32.EnumDisplaySettings(null, User_32.ENUM_CURRENT_SETTINGS, ref dm))
  107.            {
  108.  
  109.                dm.dmPelsWidth = width;
  110.                dm.dmPelsHeight = height;
  111.  
  112.                int iRet = User_32.ChangeDisplaySettings(ref dm, User_32.CDS_TEST);
  113.  
  114.                if (iRet == User_32.DISP_CHANGE_FAILED)
  115.                {
  116.                    return "Unable To Process Your Request. Sorry For This Inconvenience.";
  117.                }
  118.                else
  119.                {
  120.                    iRet = User_32.ChangeDisplaySettings(ref dm, User_32.CDS_UPDATEREGISTRY);
  121.                    switch (iRet)
  122.                    {
  123.                        case User_32.DISP_CHANGE_SUCCESSFUL:
  124.                            {
  125.                                return "Success";
  126.                            }
  127.                        case User_32.DISP_CHANGE_RESTART:
  128.                            {
  129.                                return "You Need To Reboot For The Change To Happen.\n If You Feel Any Problem After Rebooting Your Machine\nThen Try To Change Resolution In Safe Mode.";
  130.                            }
  131.                        default:
  132.                            {
  133.                                return "Failed To Change The Resolution";
  134.                            }
  135.                    }
  136.  
  137.                }
  138.  
  139.  
  140.            }
  141.            else
  142.            {
  143.                return "Failed To Change The Resolution.";
  144.            }
  145.        }
  146.  
  147.        private static DEVMODE1 GetDevMode1()
  148.        {
  149.            DEVMODE1 dm = new DEVMODE1();
  150.            dm.dmDeviceName = new String(new char[32]);
  151.            dm.dmFormName = new String(new char[32]);
  152.            dm.dmSize = (short)Marshal.SizeOf(dm);
  153.            return dm;
  154.        }
  155.    }
  156. }
  157.  
  158. "@
  159.  
  160. Add-Type $pinvokeCode -ErrorAction SilentlyContinue
  161. [Resolution.PrmaryScreenResolution]::ChangeResolution($width,$height)
  162. }
  163.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement