ps/Modules/Alkami.PowerShell.Common/Public/Add-DirectoryToPath.ps1
2023-05-30 22:51:22 -07:00

34 lines
1.0 KiB
PowerShell

function Add-DirectoryToPath {
<#
.SYNOPSIS
Adds a Path to the System Path Ennvironment Variable
#>
[CmdletBinding()]
Param(
[Parameter(Mandatory = $true)]
[string]$directory
)
$logLead = (Get-LogLeadName);
if (!(Test-Path $directory)) {
Write-Warning ("$logLead : Path {0} does not exist, it will not be added to the PATH variable" -f $directory)
return
}
$currentPathValue = [Environment]::GetEnvironmentVariable("Path", [System.EnvironmentVariableTarget]::Machine)
Write-Verbose ("$logLead : Current Path Value: {0}" -f $currentPathValue)
if ($currentPathValue -like ("*{0}*" -f $directory)) {
Write-Warning ("$logLead : Directory {0} already exists in the PATH variable" -f $directory)
return
}
$newPathValue = $currentPathValue.TrimEnd(";") + ";" + $directory
Write-Output ("$logLead : Updating Path Value to {0}" -f $newPathValue)
[Environment]::SetEnvironmentVariable("Path", $newPathValue, [System.EnvironmentVariableTarget]::Machine) | Out-Null
}