Page 1 of 1

[SOLVED] Query if battery is installed.

Posted: 07 Nov 2019 10:03
by PAB
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.

Re: Query if battery is installed.

Posted: 07 Nov 2019 11:14
by aGerman
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

Re: Query if battery is installed.

Posted: 07 Nov 2019 12:52
by PAB
Brilliant Steffen, it is appreciated!