Scripts/Remove-BPAWorkflowVariable.ps1

function Remove-BPAWorkflowVariable {
    <#
        .SYNOPSIS
            Removes a shared variable from a BPA workflow
 
        .DESCRIPTION
            Remove-BPAWorkflowVariable can remove shared variables from a workflow object.
 
        .PARAMETER InputObject
            The object to remove the variable from.
 
        .PARAMETER Name
            The name of the variable to remove.
 
        .INPUTS
            The following BPA object types can be modified by this function:
            Workflow
            WorkflowVariable
 
        .EXAMPLE
            # Remove variable 'emailAddress' from workflow 'Some Workflow'
            Get-BPAWorkflow "Some Workflow" | Remove-BPAWorkflowVariable -Name "emailAddress"
 
        .NOTES
            Author(s): : David Seibel
            Contributor(s) :
            Date Created : 07/07/2017
            Date Modified : 02/08/2018
 
        .LINK
            https://github.com/davidseibel/PoshBPA
    #>

    [CmdletBinding(SupportsShouldProcess=$true,ConfirmImpact='Medium')]
    param(
        [Parameter(Mandatory = $true, ValueFromPipeline = $true)]
        $InputObject,

        [ValidateNotNullOrEmpty()]
        [string]$Name
    )
    PROCESS {
        foreach ($obj in $InputObject) {
            $shouldUpdate = $false
            switch ($obj.TypeName) {
                "Workflow" {
                    $update = Get-BPAWorkflow -ID $obj.ID -BPAServer $obj.BPAServer
                    if ($update.Variables.Name -contains $Name) {
                        $update.Variables = @($update.Variables | Where-Object {$_.Name -ne $Name})
                        $shouldUpdate = $true
                    }
                }
                "WorkflowVariable" {
                    $update = Get-BPAWorkflow -ID $obj.ParentID -BPAServer $obj.BPAServer
                    $update.Variables = @($update.Variables | Where-Object {$_.ID -ne $obj.ID})
                    $shouldUpdate = $true
                }
                default {
                    Write-Error -Message "Unsupported input type '$($obj.TypeName)' encountered!" -TargetObject $obj
                }
            }

            if ($shouldUpdate) {
                $update | Set-BPAObject
            } else {
                Write-Verbose "$($obj.TypeName) '$($obj.Name)' does not contain a variable named '$Name'."
            }
        }
    }
}