How to set/get environment variables

Hi I wanna setup an environment variable which I wanna use it in specific robot
I’ve read docs but the docs have old screenshot or documentation because there’s no environment option appearing.

we assume you are talking about windows environment vars like: PATH
Let us know the following:

  • which sample environment variable
  • selected targetframework (legacy / windows)
  • UiPath.System.Activities version used in the project

Had you tried:
Environment.GetEnvironmentVariable(“PATH”)

invoke method:
Environment
Method: SetEnvironmentVariable
Params: Varname, VarValue - all as of string

When Environment is meant in sense of ORCHESTRATOR (your Tag on title) kindly note:

it is related to classic and maybe your scenario is set to modern folder

As a third possible interpretation have also a look here

Any reason you want to use environment variables and not assets?

For environment variables there are activities from UiPath already to help read them.

1 Like

Hi @swapnil.soni,

If you do want to use environment variables remember that the host pc / robot machine will need to be restarted.

Read more on this behaviour in this thread : How to access manually created environment variables from uipath? - Help / Activities - UiPath Community Forum

So setting enviornment variables via the robot and accessing them without a restart can introduce failure in your automation.

  1. Set enviornment variable via PowerShell
    About Environment Variables - PowerShell | Microsoft Learn

    [Environment]::SetEnvironmentVariable('Foo','')

    Or as @ppr noted using Invoke Code activity to set the variable

  2. Restart robot machine, not just log off, a restart.

  3. Use Get Environment Variable activity from UiPath


There is also a neat little workaround if you do not want to restart the robot PC

Alternative to restart would be to kill Explorer process from User Ron AndKim Stengel - Server Fault
Refer the thread here : How do you add a Windows environment variable without rebooting? - Server Fault


Lastly, as @Jon_Smith noted, why not use assets in the orchestrator, easier to edit when needed and can be allocated to different robot users and you avoid having to test all of the steps above.

You can Set Asset and Get Asset in the same runtime without any workarounds. Do consider this approach (using orcehstrator assets) over others.

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