liveBook · Manning

PSCredential

As explained in Practical Automation with PowerShell: Effective scripting from the console to the cloud

A credential object is a structured PowerShell value that packages an identity and its password or other secret for authentication.

PSCredential packages a username with a SecureString password so PowerShell commands can receive authentication data without passing ordinary password text. It fits automations that cross multiple environments and need credentials, API keys, or other secrets during execution. section 4.2 section 4.2.2

Create and use the object according to the authentication boundary:

  • Prompt interactively with Get-Credential when a user can supply the identity and secret at run time. section 4.2.2
  • Construct it manually from a username and SecureString when another system supplies the values; recognize that unsecured input copies remain in memory. section 4.2.2
  • Pass it to commands that support PSCredential, such as a SQL diagnostic query, rather than converting it unnecessarily. section 4.4.2
  • Convert it to a .NET NetworkCredential for external systems requiring that type, while recognizing that the converted password is plain text until the session closes. section 4.2.2

Rebuilding a Jenkins credential

Jenkins exposes bound values as environment-variable strings, so convert the password back to SecureString and create a PSCredential before passing it as SqlCredential. Bind sqlusername and sqlpassword to a stored username-and-password credential, then use sqlserver for the target instance. section 4.4.2

$Credential = Get-Credential

Use the compact interactive pattern when prompting for credentials is appropriate. section 4.2.2

$Username = 'Contoso\BGates'
$Password = ConvertTo-SecureString 'Password' -AsPlainText -Force
$Credential = New-Object System.Management.Automation.PSCredential $Username, 
 $Password
$NetCred = $Credential.GetNetworkCredential()
$NetCred
UserName    Domain
--------    ------
BGates      Contoso

Compare manual PSCredential construction with conversion to NetworkCredential for systems that require a .NET credential object. section 4.2.2

How Practical Automation with PowerShell: Effective scripting from the console to the cloud covers it

How deep it goes
Explains structure, interactive and manual construction, conversion boundaries, and Jenkins reconstruction, but does not detail credential-vault implementation.
Examples
Uses interactive prompting, manual construction, NetworkCredential conversion, and Jenkins-backed SQL health checks as concrete examples.
What it assumes
Assumes PowerShell automation familiarity while teaching the credential-object structure and SecureString relationship directly.
What you can do after
Apply it by creating PSCredential values, passing them to supported commands, or reconstructing them from Jenkins environment variables.
How it connects
Connects PSCredential with SecureString, Get-Credential, NetworkCredential, SecretManagement, and least-privilege security.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Practical Automation with PowerShell: Effective scripting from the console to the cloud. The section links point to the parts of the book it draws on.