Verify Active Directory or Local SAM store credentials.
This function takes a credential object (user name and password) as input and will verify if the combination is correct.
The function returns a boolean based on the result.
The script defaults to local user accounts, but a remote computername can be specified in the -ComputerName
parameter.
If a domain is specified with the user name, the domain will be used to validate the supplied credentials.
# Install and import from PowerShell Gallery before using:
Install-Module Test-Credential
Import-Module Test-Credential
# Test the credential object: $creds
Test-Credential $creds
- Type:
[SecureString]
The credentials obejct to verify. Credential objects are usually collected from a user:
$creds = Get-Credential
However, you might need to build the credentials object in the script:
$username = 'VertigoRay'
$plainPassword = 'P@$$w0rd'
$securePassword = $PlainPassword | ConvertTo-SecureString -AsPlainText -Force
$creds = New-Object System.Management.Automation.PSCredential -ArgumentList $username, $securePassword
If you want to validate agains Active Directory, the user name should include the domain:
$username = 'VertigoRay'
- Type:
[String]
- Default:
$env:ComputerName
The computer on which the local credentials will be verified. Only necessary when verifying a Local SAM store on a different machine.
If a domain is specified with the user name, this parameter will be ignored.
$creds = Get-Credential
$creds
UserName Password
-------- --------
vertigoray System.Security.SecureString
Test-Credential -Credentials $creds
$username = 'VertigoRay'
$plainPassword = 'P@$$w0rd'
$securePassword = $PlainPassword | ConvertTo-SecureString -AsPlainText -Force
$creds = New-Object System.Management.Automation.PSCredential -ArgumentList $username, $securePassword
Test-Credential -Credentials $creds
$creds = Get-Credential
$creds
UserName Password
-------- --------
vertigoray System.Security.SecureString
Test-Credential -Credentials $creds -ComputerName 'MyComputer02'
$username = 'VertigoRay'
$plainPassword = 'P@$$w0rd'
$securePassword = $PlainPassword | ConvertTo-SecureString -AsPlainText -Force
$creds = New-Object System.Management.Automation.PSCredential -ArgumentList $username, $securePassword
Test-Credential -Credentials $creds -ComputerName 'MyComputer02'
$creds = Get-Credential
$creds
UserName Password
-------- --------
DOM\vertigoray System.Security.SecureString
Test-Credential -Credentials $creds
$username = 'DOM\VertigoRay'
$plainPassword = 'P@$$w0rd'
$securePassword = $PlainPassword | ConvertTo-SecureString -AsPlainText -Force
$creds = New-Object System.Management.Automation.PSCredential -ArgumentList $username, $securePassword
Test-Credential -Credentials $creds
Inspired by:
Improvements made because:
- Wanted a single function to rule them all.
- Use a secure Credential Object.
- Improved error messages for function, instead of interior methods.
- Deployable via PSGallery