Add-2SystemPath.ps1


<#PSScriptInfo
 
.VERSION 1.1
 
.GUID ba8a6268-80fe-479e-828b-be055f1636b1
 
.AUTHOR Kalichuza
 
.COMPANYNAME Kalichuza
 
.COPYRIGHT
 
.TAGS
 
.LICENSEURI
 
.PROJECTURI
 
.ICONURI
 
.EXTERNALMODULEDEPENDENCIES
 
.REQUIREDSCRIPTS
 
.EXTERNALSCRIPTDEPENDENCIES
 
.RELEASENOTES
 
 
.PRIVATEDATA
 
#>


<#
 
.DESCRIPTION
 Takes a file path argument to add a file to the system path. Thus, allowing it the be run from any Dir in the terminal.
 
#>
 

Param()


<#PSScriptInfo
  
.VERSION 1.1
  
.GUID 1faf3ff1-1702-45f3-8afe-af54f4df4497
  
.AUTHOR Kalichuza
  
  
.EXTERNALMODULEDEPENDENCIES
  
.REQUIREDSCRIPTS
  
.EXTERNALSCRIPTDEPENDENCIES
  
.RELEASENOTES
  
  
#>


<#
  
.DESCRIPTION
 Takes a file path argument to add a file to the system path. Thus, allowing it the be run from any Dir in the terminal.
  
#>


param (
    [Parameter(Mandatory)]
    [string]$Path2Folder
)

function Add-2SystemPath {
    
    # Get the current system PATH variable
    $currentPath = [System.Environment]::GetEnvironmentVariable("Path", [System.EnvironmentVariableTarget]::Machine)

    # Check if the path is already present
    if ($currentPath -split ';' -contains $Path2Folder) {
        Write-Host "Path already exists in the system PATH." -ForegroundColor Yellow
        return
    }

    # Append the new path
    $newPath = "$currentPath;$Path2Folder"

    try {
        # Set the updated PATH variable in the machine environment
        [System.Environment]::SetEnvironmentVariable("Path", $newPath, [System.EnvironmentVariableTarget]::Machine)

        # Refresh the current session's PATH variable
        $env:Path = $newPath

        Write-Host "Successfully added '$Path2Folder' to the system PATH." -ForegroundColor Green
    }
    catch {
        Write-Host "Failed to update system PATH. Error: $($_.Exception.Message)" -ForegroundColor Red
    }
}

# Call the function with the provided parameter
Add-2SystemPath -Path2Folder $Path2Folder