andreaspfr
andreaspfr

Reputation: 2314

How to authenticate an user in ActiveDirectory with powershell

I would like to authenticate an user in my ActiveDirectory with the Username and the Password. Is there any chance to do that with powershell and the activeDirectory module. Thank you

Upvotes: 17

Views: 50184

Answers (3)

E.R.Z.
E.R.Z.

Reputation: 1

here is my version of the script. In this vesion the credentials are not stored in plain text, when you run the function it will prompt you to enter the credentials and

Function Test-ADAuthentication { $Cred = Get-Credential (New-Object DirectoryServices.DirectoryEntry "",$($Cred.UserName),$($cred.GetNetworkCredential().password)).psbase.name -ne $null }

If pass is wrong return false, if pass is right return true

Upvotes: 0

Shay Levy
Shay Levy

Reputation: 126732

Requires .NET 3.5 and PowerShell V2

$UserName = 'user1'
$Password = 'P@ssw0rd'
$Domain = $env:USERDOMAIN

Add-Type -AssemblyName System.DirectoryServices.AccountManagement
$ct = [System.DirectoryServices.AccountManagement.ContextType]::Domain
$pc = New-Object System.DirectoryServices.AccountManagement.PrincipalContext $ct,$Domain
$pc.ValidateCredentials($UserName,$Password)

Upvotes: 20

CosmosKey
CosmosKey

Reputation: 1317

There are multiple ways of doing this. Here is a quick and simple function which authenticates a user to AD.

Function Test-ADAuthentication {
    param($username,$password)
    (new-object directoryservices.directoryentry "",$username,$password).psbase.name -ne $null
}

PS C:\> Test-ADAuthentication "dom\myusername" "mypassword"
True
PS C:\> 

It might not be the best function for your needs but your question lacks details.

Upvotes: 29

Related Questions