This solution allows you to use the batch file as a single script to adjust screen resolution without juggling multiple files, simplifying execution and possibly distribution. Make sure that the details of the function Set-ScreenResolution are correctly translated into the batch file context to avoid runtime errors.
You can integrate the content of your 1080.ps1 script directly into a batch file using a technique called "script block" where you directly pass PowerShell commands as a block of text with the -Command parameter in the powershell.exe command line. This will let you eliminate the .ps1 file and have everything in one .bat file.
Given your scenario, assuming you have the necessary PowerShell function to change the screen resolution (Set-ScreenResolution) already defined elsewhere or it's a straightforward function, here’s how you can combine everything into a single .bat file:
@echo off
Powershell.exe -NoProfile -ExecutionPolicy Bypass -Command "
function Set-ScreenResolution
{
Param ([int]$width, [int]$height, [int]$refreshRate)
Add-Type -AssemblyName System.Windows.Forms
$Screen = [System.Windows.Forms.Screen]::AllScreens
[Resolution.Resolution]::SetResolution($width, $height, $refreshRate)
}
Set-ScreenResolution -width 1920 -height 1080 -refreshRate 60
"
Steps to Integrate in .bat File
-
Paste the PowerShell Script Directly: You add the content of your Set-ScreenResolution.ps1 into the command block of the Powershell.exe call. You need to define the Set-ScreenResolution function with the actual implementation you have which should include the logic to change the screen resolution of the Windows system.
-
Adjust Command Format: Make sure that the PowerShell command text in the .bat file is enclosed in quotes and that all necessary PowerShell syntax (such as function and parameter definitions) is correctly portrayed.
Important Notes
-
Correct Function Implementation: It's important to ensure that your function, Set-ScreenResolution, is implemented correctly. The snippet shown assumes that you have the needed .NET assemblies and perhaps a custom C# code or similar methods for changing resolutions (this might need proper rights or permissions on the machine).
-
Security and Execution Policy: The -ExecutionPolicy Bypass flag is used here to help ensure the script runs without administrative blocks. Still, it should be used with consideration of security implications.
This method allows you to consolidate your scripts into a single batch file, which can be beneficial for simplicity in management and execution. Test the combined script thoroughly to ensure that there are no syntax or execution errors, especially when translating from a PowerShell file to an embedded command within a batch file.