Scripts/Remove-BPACredentialStoreItem.ps1

function Remove-BPACredentialStoreItem {
    <#
        .SYNOPSIS
            Removes credentials for the specified BPA server from the credential store.
 
        .DESCRIPTION
            Remove-BPACredentialStoreItem removes credential objects from the credential store file.
 
        .PARAMETER BPAServer
            The AutoMate BPA management server to remove from the credential store.
 
        .PARAMETER FilePath
            The file the server/credential combinations are stored in. It is stored in the user profile by default.
 
        .EXAMPLE
            Remove-BPACredentialStoreItem -BPAServer server01
 
        .NOTES
            Author(s): : David Seibel
            Contributor(s) :
            Date Created : 05/04/2017
            Date Modified : 05/01/2018
 
        .LINK
            https://github.com/davidseibel/PoshBPA
    #>

    [CmdletBinding()]
    param(
        [Parameter(Mandatory = $true, ValueFromPipeline = $true)]
        [ValidateNotNullOrEmpty()]
        $CredentialStoreItem
    )
    BEGIN {
        $removedItemCount = 0
    }

    PROCESS {        
        foreach ($item in $CredentialStoreItem) {
            if ($item.TypeName -eq "CredentialStoreItem") {
                if (Test-Path -Path $item.FilePath) {
                    $credStoreItems = Import-Clixml -Path $item.FilePath
                    if ($null -ne $credStoreItems[$item.BPAServer]) {
                        $credStoreItems.Remove($item.BPAServer)
                        Write-Verbose "Removed credential store item for server '$($item.BPAServer)'."
                        $removedItemCount++
                    }
                    $credStoreItems | Export-Clixml -Path $item.FilePath
                }
            } else {                    
                Write-Error -Message "Unsupported input type '$($item.TypeName)' encountered!" -TargetObject $item
            }
        }
    }

    END {
        Write-Verbose "Removed $removedItemCount items."
    }
}