Search code examples
powershellbatch-filecmd

How to count number of screens via powershell or cmd


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

Solution

  • 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.