I was wondering if it was possible to force a laptop running Windows to use its battery as the power source even when a charger is plugged in? If so, is it possible to switch between power sources programmatically?
Good question but the answer is very simple.
laptops are designed to use the
battery only and few can also power on with
AC Charger -->
AC Charger -->
You can do a simple test:
Laptop Battery and only connect
AC Charger and try to power on, see if it works. You can repeat this experiment with couple of Laptop models to get to know.
Windows does not support this natively. There are some laptops where software powermanagement software is shipped along with the laptop that offers control to do exactly what you request. It'll run from the battery disregarding the charger until a certain percentage has been met and then charges the battery again.
One brand I know that does this is Lenovo. I don't know if all their laptops have this function, but I've seen it on one and played with it. The biggest concern with this feature was, that when it switches to battery power, it also wants to do power management functions, such as: not use the CPU and GPU fully, so playing fast games was not practical. In the end, we decided to not use this functionality for the following reason:
If you want to wear down the battery less because you will mostly be running on AC power, simply consider removing the battery altogether and only use it with AC power.
If you want to be able to have the luxury of continuing to work while there is a power outage, you will need to have the battery inserted all the time. You might want to consider purchasing a 2nd battery and just let the current one wear out. You can change batteries if you want to actually use the battery on its fullest.
Note that I always use my laptop with charger and my battery has worn out a bit, and still I have over an hour of battery usage on a full charge.
A bit more related to your question: Windows cannot do it, but there might be software available that can do it. I don't know software that offers this though, and SuperUser is not about requesting software packages either, so I can't help you on that part, but google might help you find a suitable software package. You would want to look for
Powermanagement download battery something along these lines.