Sheraram_Prajapat
Sheraram_Prajapat

Reputation: 597

Powershell check if user is local-user and have admin rights from username and password on local windows machine (Not active directory)

I want to write a PowerShell script that takes username and password as input and then it checks if a user with those credentials exists and has admin rights.

Most of the questions or articles I have seen are about the active directory. I'm not talking about the active directory. I just want to check for a normal local machine.

I've tried this but I think this is about the active directory too.

$username = read-host 'Enter username'
$password = read-host 'Enter pass'

$computer = $env:COMPUTERNAME

Add-Type -AssemblyName System.DirectoryServices.AccountManagement
$obj = New-Object System.DirectoryServices.AccountManagement.PrincipalContext('machine', $computer)
$obj
$obj.ValidateCredentials($username, $password) 

Upvotes: 0

Views: 2556

Answers (1)

RetiredGeek
RetiredGeek

Reputation: 3158

You don't even need the password only the Userid using the microsoft.powershell.localaccounts module.

Function Test-Admin {

#Note: this requires that the module micorosft.powershell.localacounts
#      be loaded BEFORE the function is run as including a required directive
#      would cause the program to fail if the program loads the module!

  Param (
   [Parameter(Mandatory=$True)]
     [String] $UserToFind 
  )

$Admins = Get-LocalGroupMember -Group Administrators

If ( $Null -eq $($Admins.Name -like "*$UserToFind*") ) { 
        $Status = ' Not ' }
Else  { $Status = ' '     }

  "$UserToFind is" + $Status + "an Administrator of this Machine"

} #End Test-Admin

Sample runs:

PS> Test-Admin -UserToFind ImAnAdmin
ImAnAdmin is an Administrator of this Machine

PS> Test-Admin -UserToFind ImNotAnAdmin
ImNotAnAdmin is Not an Administrator of this Machine

Note: user names above replaced.

HTH

Upvotes: 1

Related Questions