Home > Mobile >  How to count number of screens via powershell or cmd
How to count number of screens via powershell or cmd

Time:11-25

I wanted to count the number of screens detected via powershell/cmd.

got the below to work but I wanted to count the number of screens detected as numeric.

@for /F "usebackq tokens=2 delims=: " %i IN (`powershell.exe Get-CimInstance -Namespace root\wmi -ClassName WmiMonitorBasicDisplayParams ^| findstr /r /C:"Active"`) do @echo %i 

this returns the below suppose I have 2 screens connected. Would anyone help me change the output to the number of screens active instead which is true to numeric/or count the number of true? i.e = 2

True 
True

CodePudding user response:

You already have the solution, you're just mixing a batch syntax with PowerShell for no reason.

@(Get-CimInstance -Namespace root\wmi -ClassName WmiMonitorBasicDisplayParams).Length is a pure PowerShell solution.

If you want to filter it to only active displays, then @(Get-CimInstance -Namespace root\wmi -ClassName WmiMonitorBasicDisplayParams | where-object { $_.Active }).Length.

CodePudding user response:

Use internal utilities if you plan to use batch-file only.

FOR /F "delims=:" %%a in ('wmic /namespace:\\root\wmi  path WmiMonitorBasicDisplayParams  get Active ^|findstr /n "Active"') DO @echo %%a

P.S. But rememeber wmic.exe has no longer support.

  • Related