Tag Archives: productivity

Read env variables with PowerShell from env files

Automation is not a panacea. It is a tool to amplify human abilities.

Carin Taylor

When developing/testing solutions you often need to provide configuration settings for different environments – usually stage, dev, production. You can of course store settings to an application, but then you need to deploy an updated version of the application for a different environment, which can be cumbersome and a nightmare for DevOps processes. Twelve-Factor app methodology for building SaaS apps talks about configuration settings benefits and what/how to apply more in detail here.

ENV files

An .env file is a plain text configuration file that stores environment variables. Environment variables are normal variables except they exist and are used by our system. We can create our own environment variables in our programs to avoid hardcoding sensitive information and compromising our apps. .env files are used to store those variables and they should be written in all uppercase separated with underscores for naming convention.

When using source control like git, make sure you don’t add them in. Read more about this here.

Example of env file content (gist available here):

In development environments you already have mechanisms for how to add different settings and set environment variables with integrated tooling (example app settings in Visual Studio, run/debug configuration in JetBrains Rider, …).

But what if you prepare automation for deployment, different environment setup, do initial setup, etc., In that case you will need to setup environment variables manually… or with CLI or script execution system like PowerShell.

PowerShell way

To set the environment variable in PowerShell we have diverse ways to do achieve the same result – more here (variable syntax, Environment provider, etc.).

In my example I’ll use simple env variable syntax. For example:

Set and read env variables

To be able to set the variables, we first need to read the file. PowerShell has a built-in cmdlet to work with files Get-Content.

We need a test file (you can use New-Item cmdlet to create file – content is available here):

Powershell cat of test env file

If we use the Get-Content on that file, we get the same result as cat previously (that is because cat is an alias for Get-Content):

Get content from file

What we need to do now is to read line by line and set the environment variables, which can be easily achieved by Foreach-Object cmdlet and split function:

Read and set environment variables

To make it more efficient, you can easily create a function (gist available here):

With that in place I can easily add path to the script function inside PowerShell profile and have it available anytime I need to run it.

Something like this:

PowerShell function output

Conclusion

Env files are a fantastic way to define different environments with different settings without changing the application each time we need to test in a unique environment. With PowerShell we can achieve automation with a few lines of code and by adding them to our env system path we can invoke it whenever we need new setup and environment variables without manual effort.

Just entries in file, call to function and we are good to go.