Home > OS >  script powershell to count the localusers
script powershell to count the localusers

Time:07-15

cls
$users= get-localuser
$i=0
$inactiv=0
Write-Host ***** stat of accounts *****
foreach ($user in $users)
     
      {
         if ($user.enabled -eq 'true')
           
           {
            Write-Host  account $user.name is active}
              
              elseif ($user.Enabled -eq 'false')
             { $inactiv= $inactiv  $users[$i]
               $i =1 
               write-host the number of inactive accounts is $inactif   }
             }

CodePudding user response:

If you want to list the users who are active, and inactive by their Enabled status property, using the same looping logic you can use:

Get-LocalUser | 
    ForEach-Object -Begin {
        $active, $inactive = 0
    } -Process {
        if ($_.Enabled) {
            $active  
            "User [{0}] is active." -f $_.Name
        }
        else {
            $inactive  
            "User [{0}] is inactive." -f $_.Name
        }
    } -End {
        "Total of active users is: $active"
        "Total of inactive users is: $inactive"
    }

Without more clarification or code errors you may be getting, this should give you the expected results according to your comments. Using the .Where({}) operator you can get the number of active/inactive account as well:

$localusers = Get-LocalUser
$localUsers.Enabled.Where{$_ -eq $true}.Count
$localUsers.Enabled.Where{$_ -eq $false}.Count

Also, you should be comparing boolean values to other boolean values and not strings.

  • Related