简体   繁体   中英

PowerShell is slow when i want to run a simple script via keyboard command/shortcut. Any way to let it run faster?

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.

  • 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:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM