Public/Remove-MaskMapping.ps1

function Remove-MaskMapping {
    <#
    .SYNOPSIS
        Removes one or more entries from the mapping file.

    .DESCRIPTION
        Removes the specified key(s) from the mapping JSON file used by Protect-SensitiveData.
        Keys that are not present produce a warning. Accepts keys from the pipeline.

    .PARAMETER Key
        One or more keys (original terms) to remove. Accepts pipeline input.

    .PARAMETER MappingFile
        Path to the mapping JSON file. Defaults to MaskSensitiveData_Map.json in the user's profile.

    .PARAMETER PassThru
        Return the removed Key/Value entries as objects.

    .EXAMPLE
        Remove-MaskMapping -Key 'Contoso'

        Removes the Contoso mapping from the default mapping file.

    .EXAMPLE
        'john','Contoso' | Remove-MaskMapping

        Removes multiple mappings piped in by key.

    .LINK
        Add-MaskMapping
    .LINK
        Get-MaskMapping
    #>

    [CmdletBinding(SupportsShouldProcess)]
    param(
        [Parameter(Mandatory, Position = 0, ValueFromPipeline, ValueFromPipelineByPropertyName)]
        [string[]]$Key,

        [string]$MappingFile = (Join-Path $env:USERPROFILE 'MaskSensitiveData_Map.json'),

        [switch]$PassThru
    )

    begin {
        $map     = Import-Mapping -MappingFile $MappingFile -Require
        $changed = $false
    }

    process {
        foreach ($k in $Key) {
            if (-not $map.ContainsKey($k)) {
                Write-Warning "Key '$k' not found in '$MappingFile'."
                continue
            }

            if ($PSCmdlet.ShouldProcess($MappingFile, "Remove mapping '$k' -> '$($map[$k])'")) {
                $removed = $map[$k]
                $map.Remove($k)
                $changed = $true
                if ($PassThru) { [pscustomobject]@{ Key = $k; Value = $removed } }
            }
        }
    }

    end {
        if ($changed -and $PSCmdlet.ShouldProcess($MappingFile, 'Save mapping file')) {
            Export-Mapping -Map $map -MappingFile $MappingFile
        }
    }
}