set-screenresolution.ps1 (4550B)
1 # http://blogs.technet.com/b/heyscriptingguy/archive/2010/07/07/hey-scripting-guy-how-can-i-change-my-desktop-monitor-resolution-via-windows-powershell.aspx 2 3 Function Set-ScreenResolution { 4 param ( 5 [Parameter(Mandatory=$true, 6 Position = 0)] 7 [int] 8 $Width, 9 [Parameter(Mandatory=$true, 10 Position = 1)] 11 [int] 12 $Height 13 ) 14 $pinvokeCode = @" 15 using System; 16 using System.Runtime.InteropServices; 17 namespace Resolution 18 { 19 [StructLayout(LayoutKind.Sequential)] 20 public struct DEVMODE1 21 { 22 [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)] 23 public string dmDeviceName; 24 public short dmSpecVersion; 25 public short dmDriverVersion; 26 public short dmSize; 27 public short dmDriverExtra; 28 public int dmFields; 29 public short dmOrientation; 30 public short dmPaperSize; 31 public short dmPaperLength; 32 public short dmPaperWidth; 33 public short dmScale; 34 public short dmCopies; 35 public short dmDefaultSource; 36 public short dmPrintQuality; 37 public short dmColor; 38 public short dmDuplex; 39 public short dmYResolution; 40 public short dmTTOption; 41 public short dmCollate; 42 [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)] 43 public string dmFormName; 44 public short dmLogPixels; 45 public short dmBitsPerPel; 46 public int dmPelsWidth; 47 public int dmPelsHeight; 48 public int dmDisplayFlags; 49 public int dmDisplayFrequency; 50 public int dmICMMethod; 51 public int dmICMIntent; 52 public int dmMediaType; 53 public int dmDitherType; 54 public int dmReserved1; 55 public int dmReserved2; 56 public int dmPanningWidth; 57 public int dmPanningHeight; 58 }; 59 class User_32 60 { 61 [DllImport("user32.dll")] 62 public static extern int EnumDisplaySettings(string deviceName, int modeNum, ref DEVMODE1 devMode); 63 [DllImport("user32.dll")] 64 public static extern int ChangeDisplaySettings(ref DEVMODE1 devMode, int flags); 65 public const int ENUM_CURRENT_SETTINGS = -1; 66 public const int CDS_UPDATEREGISTRY = 0x01; 67 public const int CDS_TEST = 0x02; 68 public const int DISP_CHANGE_SUCCESSFUL = 0; 69 public const int DISP_CHANGE_RESTART = 1; 70 public const int DISP_CHANGE_FAILED = -1; 71 } 72 public class PrmaryScreenResolution 73 { 74 static public string ChangeResolution(int width, int height) 75 { 76 DEVMODE1 dm = GetDevMode1(); 77 if (0 != User_32.EnumDisplaySettings(null, User_32.ENUM_CURRENT_SETTINGS, ref dm)) 78 { 79 dm.dmPelsWidth = width; 80 dm.dmPelsHeight = height; 81 int iRet = User_32.ChangeDisplaySettings(ref dm, User_32.CDS_TEST); 82 if (iRet == User_32.DISP_CHANGE_FAILED) 83 { 84 return "Unable To Process Your Request. Sorry For This Inconvenience."; 85 } 86 else 87 { 88 iRet = User_32.ChangeDisplaySettings(ref dm, User_32.CDS_UPDATEREGISTRY); 89 switch (iRet) 90 { 91 case User_32.DISP_CHANGE_SUCCESSFUL: 92 { 93 return "Success"; 94 } 95 case User_32.DISP_CHANGE_RESTART: 96 { 97 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."; 98 } 99 default: 100 { 101 return "Failed To Change The Resolution"; 102 } 103 } 104 } 105 } 106 else 107 { 108 return "Failed To Change The Resolution."; 109 } 110 } 111 private static DEVMODE1 GetDevMode1() 112 { 113 DEVMODE1 dm = new DEVMODE1(); 114 dm.dmDeviceName = new String(new char[32]); 115 dm.dmFormName = new String(new char[32]); 116 dm.dmSize = (short)Marshal.SizeOf(dm); 117 return dm; 118 } 119 } 120 } 121 "@ 122 Add-Type $pinvokeCode -ErrorAction SilentlyContinue 123 [Resolution.PrmaryScreenResolution]::ChangeResolution($width,$height) 124 }