Jump to content
Sign in to follow this  

How to set screensize in preboot / boot of windows

Recommended Posts

Hey, im looking fo an sollution for the following prob

- got several displays on one pc and every time i restart it the resolution carshs caused by 4k displays, so after every restart need to reconnect the monitors again manually.

- the cutting pc got about 10 displays all shouldl run in hd on 1920x1080


so i thought about a script like this


#Load set-screenresolution Function
#Example . c:\PowershellFunctions\set-screenresolution.ps1

. C:\ScreenResolution\Set-ScreenResolutionall.ps1

#Call Function Set-ScreenResolution & set resoltuin

Set-ScreenResolutionex 1920 1080 0
Set-ScreenResolutionex 1920 1080 1
Set-ScreenResolutionex 1920 1080 2
Set-ScreenResolutionex 1920 1080 3
Set-ScreenResolutionex 1920 1080 4
Set-ScreenResolutionex 1920 1080 5
Set-ScreenResolutionex 1920 1080 6
Set-ScreenResolutionex 1920 1080 7
Set-ScreenResolutionex 1920 1080 8
Set-ScreenResolutionex 1920 1080 9
Set-ScreenResolutionex 1920 1080 10
Set-ScreenResolutionex 1920 1080 11

and this


Function Set-ScreenResolutionEx {  
param (  
           Position = 0)]  
           Position = 1)]  
           Position = 2)]  
$Code = @"
using System;  
using System.Runtime.InteropServices;  
namespace Resolution  
    public enum DisplayDeviceStateFlags : int
        /// <summary>The device is part of the desktop.</summary>
        AttachedToDesktop = 0x1,
        MultiDriver = 0x2,
        /// <summary>The device is part of the desktop.</summary>
        PrimaryDevice = 0x4,
        /// <summary>Represents a pseudo device used to mirror application drawing for remoting or other purposes.</summary>
        MirroringDriver = 0x8,
        /// <summary>The device is VGA compatible.</summary>
        VGACompatible = 0x10,
        /// <summary>The device is removable; it cannot be the primary display.</summary>
        Removable = 0x20,
        /// <summary>The device has more display modes than its output devices support.</summary>
        ModesPruned = 0x8000000,
        Remote = 0x4000000,
        Disconnect = 0x2000000
    [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Ansi)]
    public struct DISPLAY_DEVICE  
        public int cb;
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst=32)]
        public string DeviceName;
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst=128)]
        public string DeviceString;
        public DisplayDeviceStateFlags StateFlags;
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst=128)]
        public string DeviceID;
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst=128)]
        public string DeviceKey;
    public enum ChangeDisplaySettingsFlags : uint
        CDS_NONE = 0,
        CDS_UPDATEREGISTRY = 0x00000001,
        CDS_TEST = 0x00000002,
        CDS_FULLSCREEN = 0x00000004,
        CDS_GLOBAL = 0x00000008,
        CDS_SET_PRIMARY = 0x00000010,
        CDS_VIDEOPARAMETERS = 0x00000020,
        CDS_ENABLE_UNSAFE_MODES = 0x00000100,
        CDS_DISABLE_UNSAFE_MODES = 0x00000200,
        CDS_RESET = 0x40000000,
        CDS_RESET_EX = 0x20000000,
        CDS_NORESET = 0x10000000
    public struct DEVMODE  
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]  
        public string dmDeviceName;  
        public short dmSpecVersion;  
        public short dmDriverVersion;  
        public short dmSize;  
        public short dmDriverExtra;  
        public int dmFields;  
        public short dmOrientation;  
        public short dmPaperSize;  
        public short dmPaperLength;  
        public short dmPaperWidth;  
        public short dmScale;  
        public short dmCopies;  
        public short dmDefaultSource;  
        public short dmPrintQuality;  
        public short dmColor;  
        public short dmDuplex;  
        public short dmYResolution;  
        public short dmTTOption;  
        public short dmCollate;  
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]  
        public string dmFormName;  
        public short dmLogPixels;  
        public short dmBitsPerPel;  
        public int dmPelsWidth;  
        public int dmPelsHeight;  
        public int dmPosition;
        public int dmDisplayFlags;  
        public int dmDisplayFrequency;  
        public int dmICMMethod;  
        public int dmICMIntent;  
        public int dmMediaType;  
        public int dmDitherType;  
        public int dmReserved1;  
        public int dmReserved2;  
        public int dmPanningWidth;  
        public int dmPanningHeight;  
    public enum DISP_CHANGE : int
        SUCCESSFUL = 0,
        RESTART = 1,
        FAILED = -1,
        BADMODE = -2,
        NOTUPDATED = -3,
        BADFLAGS = -4,
        BADPARAM = -5,
        BADDUALVIEW = -6
    public class User_32  
        public static extern bool EnumDisplayDevices(string lpDevice, uint iDevNum, ref DISPLAY_DEVICE lpDisplayDevice, uint dwFlags);
        public static extern int EnumDisplaySettingsEx(string lpszDeviceName, int iModeNum, ref DEVMODE lpDevMode, uint dwFlags);
        public static extern int ChangeDisplaySettingsEx(string lpszDeviceName, ref DEVMODE lpDevMode, IntPtr hwnd, ChangeDisplaySettingsFlags dwflags, IntPtr lParam);
        public const int ENUM_CURRENT_SETTINGS = -1;  
    public class ScreenResolution  
        // Arguments
        // int width : Desired Width in pixels
        // int height : Desired Height in pixels
        // int deviceIDIn : 12,  
        static public string ChangeResolution(int width, int height, int deviceIDIn)
            //Basic Error Check
            uint deviceID = 0;
            if (deviceIDIn < 0){
                deviceID = 0;
                deviceID = (uint) deviceIDIn;
            DISPLAY_DEVICE d = new DISPLAY_DEVICE();  
            d.cb = Marshal.SizeOf(d);
            DEVMODE dm = GetDevMode();
            User_32.EnumDisplayDevices(null, deviceID, ref d, 1); //Get Device Information
            // Print Device Information
            Console.WriteLine("DeviceName: {0} \nDeviceString: {1}\nDeviceID: {2}\nDeviceKey {3}\nStateFlags {4}\n", d.DeviceName, d.DeviceString, d.DeviceID, d.DeviceKey, d.StateFlags);  
            //Attempt to change settings
            if (0 != User_32.EnumDisplaySettingsEx ( d.DeviceName, User_32.ENUM_CURRENT_SETTINGS, ref dm, 0))  
                dm.dmPelsWidth = width;  
                dm.dmPelsHeight = height;  
                int iRet = User_32.ChangeDisplaySettingsEx( d.DeviceName, ref dm, IntPtr.Zero, ChangeDisplaySettingsFlags.CDS_TEST, IntPtr.Zero);  
                if (iRet == (int) DISP_CHANGE.FAILED)  
                    return "Unable To Process Your Request. Sorry For This Inconvenience.";  
                    iRet = User_32.ChangeDisplaySettingsEx(d.DeviceName, ref dm, IntPtr.Zero, ChangeDisplaySettingsFlags.CDS_UPDATEREGISTRY, IntPtr.Zero);
                    switch (iRet)  
                        case (int) DISP_CHANGE.SUCCESSFUL:  
                                return "Success";  
                        case (int) DISP_CHANGE.RESTART:  
                                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.";  
                                return "Failed To Change The Resolution.";  
                return "Failed To Change The Resolution.";  
        private static DEVMODE GetDevMode()  
            DEVMODE dm = new DEVMODE();  
            dm.dmDeviceName = new String(new char[32]);  
            dm.dmFormName = new String(new char[32]);  
            dm.dmSize = (short)Marshal.SizeOf(dm);  
            return dm;  
Add-Type $Code

theres just a little prob about the scripts, it needs to be executed before windows boots up else the displays again wont be recognised.

someone out there got an sollution for me?

Already tried some group policys but they failed.




Share this post

Link to post
Share on other sites

If you really mean "before Windows boots up" as in before system loads, then you will not be able to execute any of that. You can execute system and user login/logon scripts but they require Windows to be already up.

MsgBox('','',(StringFromASCIIArray(StringSplit('13:65:108:108:32:116' _
&':104:111:115:101:32:109:111:109:101:110:116:115:32:119:105:108:108' _
&':32:98:101:32:108:111:115:116:32:105:110:32:116:105:109:101:44:32:' _
&'108:105:107:101:32:116:101:97:114:115:32:105:110:32:114:97:105:110' _

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Create New...