This documentation provides details on three PowerShell functions for managing PSCredential objects:
New-PSCredential: Creates a newPSCredentialobject.Save-PSCredential: Saves aPSCredentialobject to a file.Restore-PSCredential: Restores aPSCredentialobject from a file.
This function generates a PSCredential object using a specified username and password.
The password can be provided as a secure string or a plain text string.
New-PSCredentialPrompts the user for a username and password and returns a PSCredential object.
New-PSCredential -Username 'admin' -Password (ConvertTo-SecureString 'P@ssw0rd!' -AsPlainText -Force)Creates a PSCredential object for the specified username and password.
This function takes a PSCredential object and exports it to a file using Export-Clixml.
If the specified file path does not exist, the function creates the necessary directory structure before saving the credential.
$credential = Get-Credential
Save-PSCredential -Credential $credential -Path 'C:\secure\credential.xml'Prompts for a username and password, then securely saves the credential to C:\secure\credential.xml.
$password = ConvertTo-SecureString 'MyPassword' -AsPlainText -Force
$credential = New-PSCredential -Username 'UserName' -Password $password
Save-PSCredential -Credential $credential -Path 'C:\secure\mycreds.xml'Saves the predefined credential to C:\secure\mycreds.xml.
The Restore-PSCredential function retrieves a PSCredential object that was previously saved using Export-Clixml.
It reads the file from the specified path and ensures the content is a valid PSCredential object before returning it.
Restore-PSCredential -Path 'C:\secure\mycredential.xml'Restores the PSCredential object from the file located at 'C:\secure\mycredential.xml'.
'C:\secure\mycredential.xml' | Restore-PSCredentialUses pipeline input to restore the PSCredential object from the specified file path.