PrivateFunctions/Rename-SlackChannel.ps1

<#
.SYNOPSIS
    This function renames a channel in Slack.
.DESCRIPTION
    This function renames a channel in Slack.
    The scope required to call this function is "channels:write".
#>

function Rename-SlackChannel {
    [CmdletBinding(PositionalBinding=$false)]
    [OutputType([Bool])]
    param(
        # The authentication token for Slack
        [Parameter(Mandatory=$true)]
        [ValidateNotNullOrEmpty()]
        [String]$token,

        # The name of the channel to rename
        [Parameter(Mandatory=$true)]
        [ValidateNotNullOrEmpty()]
        [String]$channelName,

        # The new name of the channel
        [Parameter(Mandatory=$true)]
        [ValidateNotNullOrEmpty()]
        [String]$newChannelName
    )

    # Retrieve the ID for the specified channel and verify that the channel exists
    $channelID = (Get-SlackChannel -Token $token -ChannelName $channelName).id
    if (!$channelID) {
        throw "The channel '$($channelName)' cannot be found in Slack."
    }

    # Prepare the API call parameters
    $invokeRestMethodParams = @{
        Uri     = "https://slack.com/api/channels.rename"
        Method  = "POST"
        Headers = @{
            "Content-Type" = "application/json"
            Authorization  = "Bearer $($token)"
        }
        Body    = @{
            channel  = "$($channelID)"
            name     = "$($newChannelName)"
            validate = $true
        } | ConvertTo-Json
    }

    # Invoke the call
    $response = Invoke-RestMethod @invokeRestMethodParams

    # Verify that the renaming is successful
    if ($response.ok) {
        return $true
    }
    else {
        throw "Failed to rename channel '$($channelName)' with the error message:`r`n$($response.error)."
    }
}