Public/Initialize-NAVVersion.ps1

function Initialize-NAVVersion {
    <#
    .Synopsis
    Detect and initialize NAV Version on the local machine.
 
    .Description
    Will look for a NAV installation and will set some common variables in the session context.
    #>

    [cmdletbinding()]
    param (
    )
    Process
    {
        $DynamicsNavParentFolder = "C:\Program Files\Microsoft Dynamics NAV" 
        $DynamicsNavParentFolder86 = "C:\Program Files (x86)\Microsoft Dynamics NAV"
        $DynamicsNavDataFolder = "C:\ProgramData\Microsoft\Microsoft Dynamics NAV"

        if (Test-Path $DynamicsNavParentFolder) {
            # read sub folders to find NavVersionCodes
            $NavVersionCodes = Get-ChildItem $DynamicsNavParentFolder
            
            if ($NavVersionCodes.Length -eq 1) {
                [int]$NavVersionCode = [int] $NavVersionCodes[0].Name 
                Write-Output "Version code found: $NavVersionCode"
            } elseif ($NavVersionCodes.Length -gt 1) {
                Write-Warning "Multiple versions of Dynamics NAV detected ($NavVersionCodes)."
                $maxVer = [int] $NavVersionCodes[0].Name
                foreach ($ver in $NavVersionCodes) {
                    $newVer = [int] $ver.Name
                    if ($newVer -gt $maxVer) {
                        $maxVer = $newVer
                    }
                }
                $NavVersionCode = $maxVer
                Write-Warning "The higher one ($maxVer) has been selected."
            } else {
                Write-Warning "Dynamics NAV is not installed on this host!"    
            }
        } else {
            Write-Warning "Dynamics NAV is not installed on this host!"
        }

        switch ($NavVersionCode) {
            71  { 
                $NavVersion="2013R2"
                $NavDefaultDatabase = 'Demo Database NAV (7-1)'         
            }
            80  { 
                $NavVersion="2015"
                $NavDefaultDatabase = 'Demo Database NAV (8-0)'
            }
            90  { 
                $NavVersion="2016"
                $NavDefaultDatabase = 'Demo Database NAV (9-0)'
            }
            100 { 
                $NavVersion="2017"
                $NavDefaultDatabase = 'Demo Database NAV (10-0)'
            }
            Default { Write-Warning "This version is not supported: $NavVersionCode" }
        }
        $NavDefaultInstance = "DynamicsNAV$NavVersionCode"
        $NavDefaultCompanyName = 'CRONUS International Ltd.'
        $NavDefaultLicencePath = "$DynamicsNavDataFolder\$NavVersionCode\Database\Cronus.flf" 

        Write-Output "The version $NavVersion of Microsoft Dynamics NAV has been detected."

        $DynamicsNavManagementModule = "$DynamicsNavParentFolder\$NavVersionCode\Service\Microsoft.Dynamics.Nav.Management.psm1"
        $DynamicsNavToolsModule = "$DynamicsNavParentFolder86\$NavVersionCode\RoleTailored Client\Microsoft.Dynamics.Nav.Model.Tools.psd1"

        Import-Module $DynamicsNavManagementModule
        Write-Output "The Microsoft Dynamics NAV Management module has been imported."

        Import-Module $DynamicsNavToolsModule
        Write-Output "The Microsoft Dynamics NAV Tools module has been imported."
    }
}