[SOLVED] Query if battery is installed.

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
PAB
Posts: 139
Joined: 12 Aug 2019 13:57

[SOLVED] Query if battery is installed.

#1 Post by PAB » 07 Nov 2019 10:03

Good afternoon,

I would like to know if a computer is a Desktop or a Laptop.

I have tried retrieving the information using Powershell and batch script but I am having trouble interpretting it because some code produced "No Instance(s) Available", some the number "2" etc. I am really close but can't quite get the correct answer. Basically, if the computer is a laptop I want the output to be Laptop, and if the computer is a desktop I want the output to be Desktop. I thought the battery would be a reasonable and solid item to look at for this determination!

Here is a small fraction of the code I have tried . . .

Code: Select all

For /f "Tokens=2 Delims==" %%a In ('WMIC PATH Win32_Battery Get EstimatedChargeRemaining /Value') Do Set Battery1=%%a

Code: Select all

For /f "Tokens=2 Delims==" %%a In ('WMIC PATH Win32_Battery Get Description /Value') Do Set Battery2=%%a

Code: Select all

For /f "usebackq Delims=" %%a In (`powershell -command "& {(Get-WmiObject Win32_Battery).BatteryStatus}"`) Do Set "Battery3=%%a"

Code: Select all

For /f Tokens=2 Delims='='" %%a In (`powershell -command "& {Get-WmiObject -Class Win32_Battery Description}"`) Do Set "Battery4=%%a"
Thanks in advance.
Last edited by PAB on 07 Nov 2019 14:43, edited 1 time in total.

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Query if battery is installed.

#2 Post by aGerman » 07 Nov 2019 11:14

The Win32_ComputerSystem class might be the better choice here.
https://docs.microsoft.com/en-us/window ... utersystem

Code: Select all

@echo off &setlocal
for /f "delims=" %%i in ('WMIC Path Win32_ComputerSystem GET PCSystemType /value') do for /f "tokens=2 delims==" %%j in ("%%i") do (
  if "%%j"=="1" (
    echo Desktop
  ) else if "%%j"=="2" (
    echo Laptop
  ) else (
    echo other
  )
)
pause
Steffen

PAB
Posts: 139
Joined: 12 Aug 2019 13:57

Re: Query if battery is installed.

#3 Post by PAB » 07 Nov 2019 12:52

Brilliant Steffen, it is appreciated!

Post Reply