This week I was working on a project that involved using the %PATH% environmental variable. The challenge was that I have some entries that look like this: %SystemRoot%\system32\WindowsPowerShell\v1.0\. When I try to use that path in PowerShell, it complains because it doesn’t expand %SystemRoot%. What I needed was a way to replace it with the actual value, which I can find in the ENV: PSdrive, or reference as $env:systemroot. This seems reasonable enough. Take a string, use a regular expression to find the environmental variable, find the variable in ENV:, do a replacement, write the revised string back to the pipeline. So here I have Resolve-EnvVariable.
The function takes a string as a parameter, or you can pipe into the function. The function looks to see if there is something that might be an environmental variable using a regular expression match.
If there is an extra % character in the string, this won’t work so I’m assuming you have some control over what you provide as input. Now I need to get the match value. At first I tried using the Regex object. But when faced with a string like this “I am %username% and working on %computername%” it also tried to turn % and working on% as an environmental variable. I’m sure there’s a regex pattern that will work but I found it just as easy to split the string on the % character and trim off the extra space.
Now, I can go through each value and see if there is a corresponding environmental variable.
I turned off the error pipeline to suppress errors about unfound entries. If something was found then I do a simple replace, otherwise, I re-use the original text.
In essence I am building a new string adding the replacement values or original text. When finished I can write the new string, which has the variable replacements back to the pipeline.
Finally, I can pass strings that contain environmental variables to the function.
This isn’t perfect. Look what happens if there is an undefined variable:
But as long as you are confident that variables are defined, then you can do things like this:
Download Resolve-EnvVariable and let me know what you think. The download version includes comment based help.