Skip to content

Instantly share code, notes, and snippets.

@VimalShekar
Last active January 12, 2018 15:39
Show Gist options
  • Save VimalShekar/fd1429a154f8b6843cfe6439cabfc541 to your computer and use it in GitHub Desktop.
Save VimalShekar/fd1429a154f8b6843cfe6439cabfc541 to your computer and use it in GitHub Desktop.
Checking if given username and password is valid in PowerShell
# -- Get the full script here : https://github.com/VimalShekar/PowerShell/blob/master/CheckIsUserValid_1.ps1
function IsLocalUserNamePasswordValid()
{
param(
[String]$UserName,
[String]$Password
)
$DS = New-Object System.DirectoryServices.AccountManagement.PrincipalContext('machine',$ComputerName)
$bReturn = $DS.ValidateCredentials($UserName, $Password)
return $bReturn
}
function IsDomainUserNamePasswordValid()
{
param(
[String]$UserName,
[String]$Password,
[String]$DomainName
)
$DS = New-Object System.DirectoryServices.AccountManagement.PrincipalContext('domain',$DomainName)
$bReturn = $DS.ValidateCredentials($UserName, $Password)
return $bReturn
}
#Example Usage:
#IsLocalUserNamePasswordValid -UserName TestLocalUser1 -Password Test@Pass1
#IsDomainUserNamePasswordValid -UserName TestDomUser1 -Password Test@Pass1 -DomainName testdom.local
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment