Private/Remove-specFileExtension.ps1

Function Remove-specFileExtension {
    <#
    .SYNOPSIS
        Removes the specified file extension from a file name if it exists
 
    .DESCRIPTION
        Removes the specified file extension from a file name if it exists
 
    .PARAMETER FileName
        The name of the file to remove the file extension from
 
    .PARAMETER Extension
        The file extension to remove from the file name. If not specified, removes any extension.
 
    .EXAMPLE
        Remove-specFileExtension -FileName "SPECLauncher.ps1" -Extension ".ps1"
        Returns: "SPECLauncher"
        Removes the file extension ".ps1" from the file name "SPECLauncher.ps1"
 
    .EXAMPLE
        Remove-specFileExtension -FileName "SPECLauncher"
        Returns: "SPECLauncher"
        Returns the file name "SPECLauncher" as it does not have any file extension
 
    .NOTES
        File : Remove-specFileExtension.ps1
        Author : owen.heaume
        Version : 1.2
    #>

    [cmdletBinding()]

    param(
        [Parameter(Mandatory = $true)]
        [string]$FileName,

        [Parameter(Mandatory = $false)]
        [string]$Extension
    )

    # Ensure the extension starts with a dot if it is provided
    if ($Extension -and $Extension -notlike '.*') {
        $Extension = ".$Extension"
    }

    # Remove the specified file extension if it exists and return the filename without the file extension
    if ($Extension) {
        if ($FileName.EndsWith($Extension)) {
            $FileName = $FileName.Substring(0, $FileName.Length - $Extension.Length)
        }
    } else {
        # Remove any extension if no specific extension is provided
        if ($FileName -like '*.*') {
            $FileName = $FileName.Substring(0, $FileName.LastIndexOf('.'))
        }
    }

    return $FileName
}