Set New System Environment Variable and Update or Append PATH variable

I have to do these 2 actions from PSADT- version using 3.8.4.

Set System Environment variable:
Currently I am using this method to set the variables however logging is not happening since I’m using the native PS method. Is there PSADT method I can use so that that can be logged or give me a log out put which says the action is completed successfully or Not?

[System.Environment]::SetEnvironmentVariable('TEST_LICENSE_FILE', ';', 'machine')

Update the PATH variable:
Currently I am using this below method, might be a better way from PSADT?

        ## Update Path Envirnment Variable
        # Separate multiple paths with semi-colon ;
        $NewPathsToAdd = "C:\Program Files (x86)\test vendor\test app 2021\;C:\Program Files (x86)\test vendor\test app 2020\"
        # Save this to a text file in case you need it later, for now we'll output it to the console
        $CurrentPaths = $env:path 
        Write-Host "`r`n `$CurrentPaths variable contains the following paths: $($currentPaths)" -ForegroundColor Cyan
        # Combine the old path and new path(s) into one string, you can swap these variables around if you want the new paths at the bottom
        $PathUpdate = $CurrentPaths + ";" + $NewPathsToAdd
        Write-Host "`r`n `$PathUpdate variable will load the following paths: $($PathUpdate)" -ForegroundColor Cyan
        # The last parameter ("Machine"),should be either User or Machine, depending on where you want the variable stored

Appreciate your help, thank you

Use Write-Log instead of Write-Host. Dont need a newline character there.

Thank you @luki1412 That logged the information about Path variable update.

How do I do that for Just adding a new variable? I would like to get the info whether addition was successful or Not as well

Write-Log writes to the PSADT log, what ever you want it to.
If you want to verify it happened, verify it and then write to the log.
You can verify it with GetEnvironmentVariable and splitting the string with delimiter ;
Then go through the string array it returns and check that exactly one is equal to the input path.

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