Start programme on startup, but only when on laptop power

bootscriptwindows

I have a programme called rainmeter which displays the time and date the second monitor i have plugged into my laptop which currently just runs on startup, but when I unplug my laptop and go to uni when I boot up I would prefer it if Rainmeter didnt just load up as I have to close it every single time. I've seen previously on windows 8 someone wrote a script that achieved this but it doesnt seem to work in windows 10. Any help would be appreciated

Best Answer

I don't have a battery, so this answer is written blindly, based on the article
How Can I Tell Whether a Laptop Computer is Running Off Batteries?

Create a file named, say, test.vbs and containing the following code:

Option Explicit

Dim objShell :Set objShell = CreateObject("WScript.Shell")
Dim objWMIService :Set objWMIService = GetObject("winmgmts:\\.\root\wmi")
Dim colItems :Set colItems = objWMIService.ExecQuery("Select * From BatteryStatus Where Voltage > 0")
Dim objItem :Set objItem = Nothing

For Each objItem in colItems
    If (objItem.PowerOnline = "True") Then objShell.Run "C:\Windows\notepad.exe"
Next

Run this file with and without AC power, to see if notepad is started only on AC power.

If the script fails, please let me know the error.

If the script succeeds, you may replace notepad with the full path to rainmeter and have it run on login by placing it in the Startup group at C:\Users\USER-NAME\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup.


Note: If the path to the executable contains spaces, the command needs to be written like this:

objShell.Run("""c:\Program Files\Mozilla Firefox\firefox.exe""")

Or like this:

objShell.Exec("c:\Program Files\Mozilla Firefox\firefox.exe")

See the post Launch programs whose path contains spaces.

Related Question