Public/Set-GhostMember.ps1

function Set-GhostMember {
    <#
    .SYNOPSIS
        Updates a member on the Ghost site.

    .DESCRIPTION
        Changes the properties that are passed and leaves every other property of the member as it is. Pass an
        empty string to clear the name or the note, and an empty list to take away every label or newsletter.

        -Labels and -Newsletters replace what the member has: name every label the member keeps. The values of
        -CustomField are added to the ones the member has; a value of $null clears a field.

        Unlike a post, a member is not protected against a change that somebody else made in the meantime: the
        update always applies.

    .EXAMPLE
        Set-GhostMember -Id '6b01427efe775327e09cd36d' -Name 'Jamie Larson II'

        Changes the name of the member.

    .EXAMPLE
        Get-GhostMember -Filter "email:'jamie@example.com'" | Set-GhostMember -Labels 'VIP' -Note ''

        Gives the member the label VIP as the only label and clears the note.

    .EXAMPLE
        Get-GhostMember -Filter 'label:bounced' | Set-GhostMember -Subscribed $false

        Unsubscribes every member with the label bounced from all newsletters.

    .EXAMPLE
        Set-GhostMember -Id '6b01427efe775327e09cd36d' -CustomField @{ company = 'Ghost'; phone = $null }

        Sets the custom field company and clears the custom field phone.

    .LINK
        https://docs.ghost.org/admin-api/members/updating-a-member
    #>

    [CmdletBinding(SupportsShouldProcess)]
    [OutputType('Ghost.Member')]
    param(
        # Id of the member to update.
        [Parameter(Mandatory, Position = 0, ValueFromPipelineByPropertyName)]
        [ValidateNotNullOrEmpty()]
        [string] $Id,

        # Email address of the member.
        [ValidateNotNullOrEmpty()]
        [string] $Email,

        # Name of the member. An empty string clears it.
        [string] $Name,

        # Note about the member, for the staff of the site only. An empty string clears it.
        [string] $Note,

        # Names of the labels of the member. They replace the labels the member has; an empty list takes all
        # of them away. A label that does not exist yet is created.
        [AllowEmptyCollection()]
        [string[]] $Labels,

        # Ids of the newsletters the member is subscribed to. They replace the newsletters the member has; an
        # empty list unsubscribes from all of them.
        [AllowEmptyCollection()]
        [string[]] $Newsletters,

        # $false unsubscribes the member from every newsletter, $true subscribes to the default newsletters.
        [bool] $Subscribed,

        # Values for the custom fields of members: the key of a field and its value. $null clears a field.
        [System.Collections.IDictionary] $CustomField
    )

    begin {
        Test-GhostConnection
        $resources = Get-GhostAPIData -Endpoint $MyInvocation.MyCommand.Name
    }

    process {
        try {
            $parameters = ConvertTo-GhostMemberParameter -Parameters $PSBoundParameters

            $uri = New-URIString -Endpoint $resources.URI -Parameters $parameters
            # Ghost refuses null for the name and the note of a member: an empty string clears them.
            $content = @{ BodyKeys = $resources.Body; Parameters = $parameters; Wrapper = $resources.Result }
            $body = New-BodyString @content -KeepEmptyString
            if (-not $body) {
                throw 'Nothing to update: pass at least one property of the member, such as -Name or -Labels.'
            }

            if ($PSCmdlet.ShouldProcess($Id, $resources.Description)) {
                $result = Submit-Request -Uri $uri -Method $resources.Method -Body $body -Success $resources.Success
                $result = Test-ReturnFormat -Result $result -Location $resources.Result

                Set-ObjectTypeName -InputObject $result -TypeName $resources.TypeName
            }
        }
        catch {
            $PSCmdlet.ThrowTerminatingError($_)
        }
    }
}