Wednesday, December 20, 2023

PowerShell script to update screen resolution on logon Windows Server;

 # PowerShell script to change screen resolution


# Define desired resolution

$Width = 1920

$Height = 1080


# Get the current video controller's configuration

$Display = Get-WmiObject -Namespace root\\wmi -Class WmiMonitorBasicDisplayParams


# Filter the configurations to the desired resolution

$Config = Get-WmiObject -Class Win32_VideoController | ForEach-Object {

    $_.SupportedDisplayModes | Where-Object {

        $_.VideoModeDescription -eq "$Width x $Height"

    }

}


# If the desired resolution is supported, set it

if ($Config) {

    $Config | ForEach-Object {

        $_.PSScriptRoot.SetDisplayMode($Width, $Height, 60)

    }

} else {

    Write-Host "The desired resolution is not supported."

}



Steps to use this script:

  • Modify the Resolution: Change the $Width and $Height variables to your desired screen resolution.
  • Save the Script: Save the script as a .ps1 file, for example, Set-Resolution.ps1.
  • Set Execution Policy: You might need to set the execution policy to allow the script to run. Open PowerShell as an administrator and run Set-ExecutionPolicy RemoteSigned.
  • Schedule the Script: Use the Task Scheduler to run the script at logon. Create a new task that triggers at logon and runs the script.
  • Test the Script: Test the script by running it manually in PowerShell or by logging out and back in to see if it triggers.

Important Notes:

  • Compatibility: This script may not work on all systems, as it depends on the compatibility of WMI with your hardware.
  • Permissions: Running this script might require administrative privileges.
  • Testing: Always test scripts in a controlled environment before deploying them broadly.

This script is a basic example and may need to be adapted for specific use cases or hardware configurations.

No comments: