Functions/New-SlackChannel.ps1

<#
.SYNOPSIS
    This function creates a channel in Slack.
.DESCRIPTION
    This function creates a channel in Slack.
#>

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

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

        # Select the stream where the messages will be directed.
        [Parameter(Mandatory=$false)]
        [ValidateSet("Information", "Warning", "Error", "None")]
        [String]$outputStream = "Error"
    )

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

    # Try to create the channel
    Write-Information "Creating channel '$($channelName)'."
    try {
        $response = Invoke-RestMethod @invokeRestMethodParams
    }
    catch {
        Write-OutputMessage "Exception occurred while creating the channel '$($channelName)' in Slack.`r`n$($_.Exception.Message)" -OutputStream $outputStream -ReturnMessage:$false
        return $false
    }

    # Verify that the creation is successful
    if ($response.ok) {
        Write-Information "Channel '$($channelName)' was created."
        return $true
    }
    else {
        Write-OutputMessage "Failed to create channel '$($channelName)' with the error message:`r`n$($response.error)." -OutputStream $outputStream -ReturnMessage:$false
        return $false
    }
}