title: Check local admins via Powershell
body: <p>I have a script that can check local admins and excluding the users that are expected to have the rights to be part of the Local Admin Group and display the result. Is there a way to simplify my script below?</p>
  <pre><code>    $date = Get-Date -Format &quot;MM-dd-yyy-HHmmss&quot;
      $report = &quot;C://Temp/ProvisioningForWSReport-$date.txt&quot;
      
      function LocalAdmins{
      [cmdletbinding(SupportsShouldProcess=$True,ConfirmImpact=&quot;Low&quot;)] Param([parameter(Mandatory=$false)][switch]$Enable)
      Begin {
      $writeTxt = &quot;$(Get-Date) - Currently checking if there are other users who have admin rights on the machine....&quot;;
      Write-Output $writeTxt
      Add-Content -path $report $writeTxt
      }
      Process {
      $hostname = hostname
      net localgroup Administrators &gt;&gt; C:\temp\AdminUsers.txt
      $User1 = &quot;Administrator&quot;
      $User2 = &quot;user1&quot;
      $User3 = &quot;user2&quot;
      $Text1 = &quot;Members&quot;
      $Text2 = &quot;-------------------------------------------------------------------------------&quot;
      $Text3 = &quot;The command completed successfully.&quot;
      (gc C:\temp\AdminUsers.txt ) | ? {$_.trim() -ne &quot;&quot; } | set-content C:\temp\AdminUsers1.txt 
      $LocalAdmin = Get-Content C:\temp\AdminUsers1.txt | Select-String -Pattern &quot;$User1&quot;, &quot;$User2&quot;, &quot;$User3&quot;, &quot;$Text1&quot;, &quot;$Text2&quot;, &quot;$Text3&quot; -NotMatch 
      $writeTxt = &quot;$(Get-Date) - [INFO] These are the users that have admin rights on the machine: $LocalAdmin &quot;;
      Write-Output $writeTxt
      Add-Content -path $report $writeTxt
      del C:\temp\AdminUsers.txt
      del C:\temp\AdminUsers1.txt
      }
      End {}
      }
  
      #####Execute Functions#####
      LocalAdmins
  </code></pre>
  <p>Here is the sample output of this script:</p>
  <pre><code>Checking if there are other users have the admin rights on the machine...
  01/05/2022 13:27:58 - [INFO] These are the users that have admin rights on the machine: User3 User4
  </code></pre>
  <p>Also, how can I display the result like this?</p>
  <pre><code>Checking if there are other users have the admin rights on the machine...
  01/05/2022 13:27:58 - [INFO] These are the users that have admin rights on the machine: User3, User4
  </code></pre>