Checking if a given username and password is valid in PowerShell

10 Sep 2017

Validating a given username and password is a common task in many scripts. Online, there are several examples that make use of ValidateCredentials method of System.DirectoryServices.AccountManagement.PrincipalContext class. You could create an instance of this class in PowerShell and then call the method.

Here’s my version that uses PrincipalContext class:

The only problem with this is that this class is only available from .NET version 3.5. If you want this functionality on machines where .NET 3.5 is not installed, there’s another way to do it by using Win32 API.

Here’s another version of my script that checks if the given username and password is valid by using LogonUser Win32 API. It basically imports the API and then calls it.