Public/Get-specComputerNamePrefix.ps1

Function Get-specComputerNamePrefix {
    <#
    .SYNOPSIS
        Retrieves the prefix from the computer name based on a specified delimiter ("-").
 
    .DESCRIPTION
        The Get-specComputerNamePrefix function extracts the prefix from the computer name by splitting it at the first
        occurrence of the delimiter ("-"). It then converts the extracted prefix to uppercase and returns it. If the
        computer name does not contain the specified delimiter, the function returns 1 and generates a verbose message.
 
    .PARAMETER ComputerName
        Specifies the computer name from which to extract the prefix. Default value is the local computer's name
        ($env:COMPUTERNAME).
 
    .EXAMPLE
        $prefix = Get-specComputerNamePrefix
        Retrieves the prefix from the computer name and stores it in the variable $prefix.
 
    .EXAMPLE
        $customPrefix = Get-specComputerNamePrefix -ComputerName "Custom-12345"
        Retrieves the prefix from the specified computer name "Custom-12345" and stores it in the variable $customPrefix.
 
    .NOTES
        Author : owen.heaume
        Version : 1.0
    #>


    [cmdletbinding()]

    param (
        [Parameter(Mandatory = $false)]
        [string]$ComputerName = $env:COMPUTERNAME
    )

    if ($ComputerName -notlike '*-*') {
        Write-Verbose "Computername does not contain a - in the hostname"
        return 1
    } else {
        Write-Verbose "Computername contains a - in the hostname"
        $ComputerPrefix = $ComputerName.Split('-')[0]
        Write-Verbose "ComputerPrefix is $ComputerPrefix"

        return $ComputerPrefix.ToUpper()
    }
}