GinShell.Windows/Public/Add-GsDomain.ps1

function Add-GsDomain {
    <#
    .SYNOPSIS
        Joins the local machine to an Active Directory domain.
    .PARAMETER DomainName
        The domain to join (e.g., corp.example.com).
    .PARAMETER Credential
        PSCredential with permission to join the domain.
    .PARAMETER Restart
        Restart the computer after joining.
    .EXAMPLE
        $cred = Get-Credential
        Add-GsDomain -DomainName 'corp.example.com' -Credential $cred -Restart
    #>

    [CmdletBinding(SupportsShouldProcess)]
    param (
        [Parameter(Mandatory)]
        [string]$DomainName,

        [Parameter(Mandatory)]
        [System.Management.Automation.PSCredential]$Credential,

        [switch]$Restart
    )

    try {
        Write-GsLog -Message "Preparing to join domain '$DomainName'..." -Type Info

        if ($PSCmdlet.ShouldProcess($env:COMPUTERNAME, "Join domain '$DomainName'")) {
            Add-Computer -DomainName $DomainName -Credential $Credential -PassThru -Verbose -Force -Restart:$false -ErrorAction Stop
            Write-GsLog -Message "Successfully joined domain '$DomainName'." -Type Success

            if ($Restart) {
                Write-GsLog -Message "Restarting computer..." -Type Info
                Restart-Computer -Force
            }
        }
    }
    catch {
        Write-GsLog -Message "Failed to join domain: $_" -Type Error
        throw
    }
}