# Avant
function New-PSADUserAccount
{
    Param(
        [Parameter(
            Mandatory = $true
        )]
        [string]$ID,

        [Parameter(
            Mandatory = $true
        )]
        [string]$SurName,

        [Parameter(
            Mandatory = $true
        )]
        [string]$GivenName,
        
        [Parameter(
            Mandatory = $true
        )]
        [string[]]$GroupsList,

        [Parameter(
            Mandatory = $false
        )]
        [string]$ProfilePath,

        [string]$Email,

        [Parameter(
            Mandatory = $false
        )]
        [string]$PhoneNumber,
        
        [Parameter(
            Mandatory = $true
        )]
        [string]$City,

        [Parameter(
            Mandatory = $true
        )]
        [string]$Department
    )

    # creation de la hashtable des donnees utilisateurs
    $userData = @{
        Department      = $Department
        City            = $City
        PhoneNumber     = $PhoneNumber
        GivenName       = $GivenName
        SurName         = $SurName
        AccountPassword = New-PSRandomPassword
        Enable          = $true
        SamAccountName  = $SurName,$ID -join ''
        EmailAddress    = $Email
    }

    # creation du ProfilePath
    $userData['ProfilePath']     = Join-Path -Path $ProfilePath -ChildPath $userData['SamAccountName']

    # creation de l'OU de destination
    $userData['Path'] = "OU=Users,OU=$City,OU=AD,OU=akalya,OU=fr"

    # creation de l'utilisateur
    $adUser = [pscustomobject]$userData | New-ADUser -PassThru

    # ajout des groupes
    Add-ADPrincipalGroupMembership -Identity $adUser -MemberOf $GroupsList
}

# Apres
function New-PSADUserAccount
{
    Param(
        [Parameter(
            Mandatory = $true
        )]
        [ValidatePattern('^\d{5}$')]
        [string]$ID,

        [Parameter(
            Mandatory = $true
        )]
        [ValidateLength(4, 40)]
        [string]$SurName,

        [Parameter(
            Mandatory = $true
        )]
        [ValidateLength(4, 40)]
        [string]$GivenName,
        
        [Parameter(
            Mandatory = $true
        )]
        [ValidateCount(1, 10)]
        [string[]]$GroupsList,

        [Parameter(
            Mandatory = $false
        )]
        [ValidateScript({
            Test-Path -Path $_ -PathType Container
        })]
        [string]$ProfilePath,

        [string]$Email,

        [Parameter(
            Mandatory = $false
        )]
        [ValidateScript({ $_ -like '0[1245]*' })]
        [string]$PhoneNumber,
        
        [Parameter(
            Mandatory = $true
        )]
        [validateSet('Paris', 'Rennes', 'Nantes', 'Lyon', 'Toulouse')]
        [string]$City,

        [Parameter(
            Mandatory = $true
        )]
        [validateSet('Comptabilite', 'Direction', 'Informatique', 'RH', 'Prod')]
        [string]$Department
    )

    # creation de la hashtable des donnees utilisateurs
    $userData = @{
        Department      = $Department
        City            = $City
        PhoneNumber     = $PhoneNumber
        GivenName       = $GivenName
        SurName         = $SurName
        AccountPassword = New-PSRandomPassword
        Enable          = $true
        SamAccountName  = $SurName,$ID -join ''
        EmailAddress    = $Email
    }

    # creation du ProfilePath
    $userData['ProfilePath']     = Join-Path -Path $ProfilePath -ChildPath $userData['SamAccountName']

    # creation de l'OU de destination
    $userData['Path'] = "OU=Users,OU=$City,OU=AD,OU=akalya,OU=fr"

    # creation de l'utilisateur
    $adUser = [pscustomobject]$userData | New-ADUser -PassThru

    # ajout des groupes
    Add-ADPrincipalGroupMembership -Identity $adUser -MemberOf $GroupsList
}

#exemple Error 
New-PSADUserAccount -City Brest `
    - Department Direction `
    - Email nicolasbdn@akalya.fr `
    - GivenName Nicolas `
    - SurName BAUDIN `
    - ID 58213 `
    - PhoneNumber 0220202020 `
    - ProfilePath '\\DataUser\Home\' `
    - GroupsList 'GG_LyonDirection'