Functions/Get-SlackUser.ps1

<#
.SYNOPSIS
    This function returns the list of users in Slack.
.DESCRIPTION
    This function returns the list of users in Slack.
    If the user name is specified, it only returns the user matching the name.
#>

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

        # The name of the user to get
        [Parameter(Mandatory=$false)]
        [String]$userName,

        # 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/users.list"
        Method  = "GET"
        Headers = @{
            "Content-Type" = "application/x-www-form-urlencoded"
            Authorization  = "Bearer $($token)"
        }
    }

    # Try to get users
    Write-Information "Retrieving users in Slack."
    try {
        $response = Invoke-RestMethod @invokeRestMethodParams
    }
    catch {
        Write-OutputMessage "Exception occurred while retrieving users in Slack.`r`n$($_.Exception.Message)" -OutputStream $outputStream -ReturnMessage:$false
        return $null
    }

    # Get the list of all users and return false if null
    $users = $response.members
    if (!$users) {
        Write-OutputMessage "Failed to retrieve users with the error message:`r`n$($response.error)." -OutputStream $outputStream -ReturnMessage:$false
        return $null
    }

    # Only return the user that matches the name, if userName is specified
    if (![String]::IsNullOrWhiteSpace($userName)) {
        return ($users | Where-Object { $_.name -eq $userName })
    }

    # Return all users
    return $users
}