3

I don't have keys on my keyboard to adjust the brightness of my screen. So i made a simple script to increase or decrease the brightness by 10%:

Brightness up: $Brigthness = Get-Ciminstance -Namespace root/WMI -ClassName WmiMonitorBrightness | select -ExpandProperty CurrentBrightness $Brigthness = $Brigthness + 10 (Get-WmiObject -Namespace root/WMI -Class WmiMonitorBrightnessMethods).WmiSetBrightness(1,$Brigthness)

For reducing the brightness i replace the + by a -.

I saved these scripts as ps1 files and made a shortcut on my desktop to open these in powershell via a keyboard command: Ctrl Shift + for increasing and Ctrl Shift - for decreasing the brightness.

Now here's the problem: when i press the command on my keyboard it takes a while for powershell to start up and run the script. It takes about 5 seconds for my screen to actually change brightness.

Is there any way to adjust the script or something to let it run faster? Just like it would on a normal keyboard shortcut. Then it's almost instant.

I was thinking about already letting powershell run in the background so that it doensn't have to boot up first, but i have no idea how.

Thanks in advance! Really curious if there's a way.

What i did so far: $Brigthness = Get-Ciminstance -Namespace root/WMI -ClassName WmiMonitorBrightness | select -ExpandProperty CurrentBrightness $Brigthness = $Brigthness + 10 (Get-WmiObject -Namespace root/WMI -Class WmiMonitorBrightnessMethods).WmiSetBrightness(1,$Brigthness)

Saved this as ps1 in my documents file.

Made a shortcut on desktop linking to the file path and assigned Ctrl Shift + as a keyboard shortcut to run it in PowerShell.

1
  • Please consider placing your code in Markdown code blocks in your question to make it easier to read. Commented Dec 5, 2022 at 21:36

3 Answers 3

0
  • The startup cost of Windows PowerShell's CLI, powershell.exe, is significant.

  • Even if you implement a custom PowerShell command server that keeps running and receives commands to execute through some IPC mechanism, you'll need some executable (and therefore launching of a new process) that the shortcut file invokes, which must request the execution of the desired command from the command server.


Therefore:

  • Using a shortcut-key mechanism that relies on shortcut files (*.lnk) is invariably slow (though with an executable that has a low startup cost it may be fast enough, depending on the use case).

  • Consider the following alternatives:

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks!! Wil try it out tomorrow on my PC. And hotkeys is the word I was looking for haha! Will let you know what worked for me!
0

If you'd like to use PowerShell you need to keep a script running continuously (or at least the console for you to enter commands).

If you don't like to have a console up and running all the time, you have to revert to some heavier methods involving running the script hidden and triggering on Windows key events or building a Systray tool with a menu...

It's quite possible, but involves a lot more coding then you might be prepared to do.

Comments

0

For anyone still looking for a solution, the fix I found was to assign the shortcut to a button on my Razer mouse with the Razer Synapse app. In Synapse, click on the button you want to change, select "Launch" and select the "Website" option. Paste the filepath from your Windows shortcut into the field; e.g.,

"%SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe -ExecutionPolicy Bypass -File "C:\\Users\\kekus\Documents\scripts\audio_switcher.ps1"

Save.
For whatever reason, the startup time is reduced to a few milliseconds.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.