Add and Remove environment variable

Hi,

Can you please help that to add and remove environment variable via PS deploy toolkit.

Thanks
Anil

Hi,

In the root of the PSAppDeployToolkit zip file, there is a file named PSAppDeployToolkit.pdf, within here you can find what Environment Variables are defined in the PSAppDeployToolkit (see Page 54 in the v3.9.3 version)

You can also set these using native PowerShell commands - here are a couple of earlier discussions around the same subject:

I hope this helps

2 Likes

I used the command [Environment]::SetEnvironmentVariable(‘PATH’, $env:Path + “;$envProgramFilesX86\JDK”,[EnvironmentVariableTarget]::Machine) But required path is appended to registry.But the issue is one more entry also added which is not required.

C:\Users\ank\AppData\Local\Microsoft\WindowsApps;C:\Program Files (x86)\JDK

That’s the problem with editing %PATH%.
it’s very easy to make code to ADD to %PATH%
It’s hard to make code that will not make %PATH% grow with with dupes and dead paths.

I tried years ago and failed.

I’m sorry I can’t remember where I copypasta’d this so I can’t give credit, but this is the function I use…

## Set PATH environment variable
Function Set-PathVariable {
    param (
        [string]$AddPath,
        [string]$RemovePath
    )
    $regexPaths = @()
    if ($PSBoundParameters.Keys -contains 'AddPath'){
        $regexPaths += [regex]::Escape($AddPath)
    }

    if ($PSBoundParameters.Keys -contains 'RemovePath'){
        $regexPaths += [regex]::Escape($RemovePath)
    }
    
    $arrPath = $env:Path -split ';'
          Write-Log -Message "Previous PATH was: $($env:Path)"
    foreach ($path in $regexPaths) {
        $arrPath = $arrPath | Where-Object {$_ -notMatch "^$path\\?"}
    }
    $PathUpdate = ($arrPath + $addPath) -join ';'
    Write-Log -Message "Updating PATH to: $($PathUpdate)"
	
	[Environment]::SetEnvironmentVariable("PATH","$PathUpdate","Machine")
}
Set-PathVariable -AddPath 'C:\Add\This\Path'
Set-PathVariable -RemovePath 'C:\Delete\This\Path'
2 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.