Functions/Authentication/PSCredential/ESS/Remove-ESSCredential.ps1

<#
.DESCRIPTION
    This script removes secured credentials (that are stored as XML-based (.ESS) text files containing their password as an encrypted standard string).
    The Metadata for preset credentials is maintained in $Global:PS_CredentialMetadata (via Import/Export-CredentialMetadata)
 
#>

function Remove-ESSCredential
{
    [CmdletBinding()]
    Param()
    DynamicParam 
    {
        # Define Credential Metadata
        $Metadata = [array]($Global:PS_CredentialMetadata)

        # Dynamic Parameter Selecting Credential Name from Metadata
        $RuntimeParameterDictionary = [System.Management.Automation.RuntimeDefinedParameterDictionary]::new()
        $RuntimeParameterDictionary.Add("Name", (New-DynamicParameter -ParamName "Name" -ValueType string -DataSet $Metadata.name -Mandatory:$true))
        $RuntimeParameterDictionary.Add("CredentialPath", (New-StaticParameter -ParamName "CredentialPath" -ValueType string -Mandatory:$false -DefaultValue $Global:PS_CredentialPath))
        $RuntimeParameterDictionary.Add("SuppressMissing", (New-StaticParameter -ParamName "SuppressMissing" -ValueType bool -DefaultValue $TRUE -Mandatory $false))

        return $RuntimeParameterDictionary
    }
    
    Begin
    {
        # Burn in parameter variables from Runtime Parameter Dictionary
        foreach ($key in $RuntimeParameterDictionary.keys){New-Variable -Name $key -Value $RuntimeParameterDictionary.$key.value}
        $CM = $Metadata | where name -eq $Name
    }

    Process
    {
        # Establish filepath
        $FilePath = "$CredentialPath\$($CM.name).ess"

        # Check for Existing Credential in the ESS Filepath
        $ESSCredential = Get-ESSCredential -Name $Name
        if ($ESSCredential)
        {
            # Remove the existing MCM Credential
            $REM = Remove-item -Path $FilePath -Force
            Write-Host "Removed credential (Name: `"$Name`" Username: `"$($ESSCredential.username))`" from $ENV:Username's MCM Cache!" -ForegroundColor Yellow
        }
        else
        {
            # If you didn't find the requested credential
            if(!$suppressmissing){Write-Host "Could not find a credential with Name: `"$Name`" in $ENV:UserName's ESS Cache" -ForegroundColor Green}
        }
    }
}