Function Get-PasswordFromCredential { <# .SYNOPSIS Gets the password from a credential object. Allows us to regulate when and where we get passwords and to pass credentials around the application. .PARAMETER Credential [PSCredential] The credential with username and password configured. #> [CmdletBinding()] Param ( [Parameter(Position = 0, ValueFromPipeline = $true, Mandatory = $true)] [PSCredential]$Credential ) $tValue1 = $null $bstr1 = [Runtime.InteropServices.Marshal]::SecureStringToBSTR($Credential.Password); try { $tValue1 = [Runtime.InteropServices.Marshal]::PtrToStringBSTR($bstr1) } finally { [Runtime.InteropServices.Marshal]::FreeBSTR($bstr1); } return $tValue1 }