Skip to content

Instantly share code, notes, and snippets.

@clr2of8
Created January 22, 2021 08:01
Show Gist options
  • Select an option

  • Save clr2of8/dc2abc271d7547fa8473f5d82c045967 to your computer and use it in GitHub Desktop.

Select an option

Save clr2of8/dc2abc271d7547fa8473f5d82c045967 to your computer and use it in GitHub Desktop.
powershell -ep bypass -command "$s=gwmi Win32_ComputerSystem; if (-not $s.PartOfDomain) { $n=-1 } else { $dr='LDAP://';$s.Domain.Split('.') | % { $dr+='DC='+$_+',' }; $dr=$dr.TrimEnd(','); try { $ad=New-Object DirectoryServices.DirectorySearcher(([adsi]$dr),'(objectCategory=computer)',('name')); $n=($ad.FindAll()).Count } catch { $n=-2} };write-host " Hosts count: " $n; "
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment