private/MDT/Get-MDTInstallDir.ps1
|
function Get-MDTInstallDir { <# .SYNOPSIS Returns the MDT installation directory from the Windows Registry. .DESCRIPTION Looks up the MDT installation directory from the Windows Registry (HKLM:\SOFTWARE\Microsoft\Deployment 4, value "Install Dir"). If the registry key is absent or the path does not exist on disk, the function falls back to the default location: C:\Program Files\Microsoft Deployment Toolkit Returns the install directory path as a string, or $null if MDT cannot be located by either method. .EXAMPLE Get-MDTInstallDir Returns 'C:\Program Files\Microsoft Deployment Toolkit' when MDT is installed at the default location. .EXAMPLE $mdtDir = Get-MDTInstallDir if ($null -eq $mdtDir) { Write-Error 'MDT is not installed.' } .INPUTS None. This function does not accept pipeline input. .OUTPUTS System.String The MDT installation directory path, or $null if not found. .NOTES Author: David Segura Company: Recast Software Registry key: HKLM:\SOFTWARE\Microsoft\Deployment 4 Registry value: Install Dir #> [CmdletBinding()] [OutputType([string])] param () $defaultPath = 'C:\Program Files\Microsoft Deployment Toolkit' $registryKey = 'HKLM:\SOFTWARE\Microsoft\Deployment 4' $registryValue = 'Install Dir' #region Check registry try { $regItem = Get-ItemProperty -Path $registryKey -Name $registryValue -ErrorAction Stop $installDir = $regItem.$registryValue.TrimEnd('\') if (Test-Path -Path $installDir) { Write-Verbose "[$(Get-Date -format s)] Found MDT install dir from registry: '$installDir'" return $installDir } Write-Verbose "[$(Get-Date -format s)] Registry value found but path does not exist: '$installDir'" } catch { Write-Verbose "[$(Get-Date -format s)] Registry key not found. Falling back to default path." } #endregion #region Fallback to default path if (Test-Path -Path $defaultPath) { Write-Verbose "[$(Get-Date -format s)] Using default MDT install dir: '$defaultPath'" return $defaultPath } #endregion Write-Warning "[$(Get-Date -format s)] MDT installation directory not found." return $null } |