title: Validating PowerShell PSCredential
body: <p>Let's say I have a <code>PSCrendential</code> object in PowerShell that I created using <code>Get-Credential</code>.</p>
  
  <p>How can I validate the input against Active Directory ?</p>
  
  <p>By now I found this way, but I feel it's a bit ugly :</p>
  
  <pre><code>[void][System.Reflection.Assembly]::LoadWithPartialName("System.DirectoryServices.AccountManagement")
  
  
  function Validate-Credentials([System.Management.Automation.PSCredential]$credentials)
  {
      $pctx = New-Object System.DirectoryServices.AccountManagement.PrincipalContext([System.DirectoryServices.AccountManagement.ContextType]::Domain, "domain")
      $nc = $credentials.GetNetworkCredential()
      return $pctx.ValidateCredentials($nc.UserName, $nc.Password)
  }
  
  $credentials = Get-Credential
  
  Validate-Credentials $credentials
  </code></pre>
  
  <p><strong>[Edit, two years later]</strong> For future readers, please note that <code>Test-Credential</code> or <code>Test-PSCredential</code> are better names, because <code>Validate</code> is not a valid powershell verb (see <code>Get-Verb</code>)</p>