Creating a Shortcut to an Environment Variable Path

If you rely on OneDrive or SharePoint to collaborate on files with your team, this tip will help you provide a uniform way to access data across all devices.

Creating a Shortcut to an Environment Variable Path

In a previous article, I wrote about how you can use environment variables to bring consistency to processes that access locally synced folders of file-sharing services, such as OneDrive:

Setting a Common Path to Sharepoint Site Folders on Multiple Devices
This quick tip will help you create consistent development environments across multiple devices and users.

I highly recommend you read that article first to provide some context for what we're trying to do here.  At a minimum, you will need to have defined an environment variable that points to some folder on your computer.  In the examples below, I will be using the FB environment variable that I created thusly:

setx FB "D:\mydomain.com\FogBugz - Documents"

In the comment section of the above article, reader Ben Sacherich posed the following question:

Can the environment variable be used in a shortcut?   We have a need to make a shortcut from one OneDrive folder to another, but the shortcut needs to work for all users.

Let's have a crack at it.

Step-by-Step

In Windows, you can [Alt]-click and drag on a folder or file to create a shortcut.  That technique won't work with environment variables.  Instead, we'll use a more manual method.

The process is pretty straightforward:

Step 1. Create the shortcut

Open File Explorer ([Win] + [E]), then navigate to the folder where you want to create your shortcut.  Press [Alt] + [H], [W], [S] OR right-click in an open area and choose New > Shortcut:

Step 2. Type the location of the item

Don't use the [Browse...] button.  Simply type the name of your environment variable enclosed in percent signs then click [Next]:

Step 3. Type a name for the shortcut

Windows will resolve the environment variable and prefill the shortcut name with the name of the target folder:

Feel free to change this name if you fancy something different.  When you're satisfied, click [Finish].

Checking Our Work

The thing I was most curious about is whether the environment variable or the absolute path would get saved in the shortcut file.  To investigate, I right-clicked on the shortcut link and checked its Properties:

Good news!  It looks like the Target value remains set to our environment variable.

I double-clicked on the shortcut link, and it took me straight to the FogBugz - Documents folder on my home computer, which is set to M:\GB_Drive\grandjean.net\FogBugz - Documents:

The location of the Sharepoint folder on my home computer.

So far, so good.

The Real Test

That's nice and all, but the real test is what happens if I try to run the shortcut on another computer where the FB environment variable points to a different folder.

I copied the shortcut file into my OneDrive folder.  I then used remote desktop to log in to my computer at the office.  After the shortcut file synced across the internet from my home computer to my office computer, I double-clicked on the file.  

Which took me straight to the FogBugz - Documents folder on my office computer, which is set to a different location than my home computer (D:\grandjean.net\FogBugz - Documents):

Success!

Shortcut to Subfolders

Yes, this works, too.

Just go into the shortcut file's Properties window and update the Target value to include one or more subfolders of the environment variable root folder:

There you have it.  Looks like this technique should work for you, Ben!  Let us know how you make out.

Image by Hands off my tags! Michael Gaida from Pixabay

All original code samples by Mike Wolfe are licensed under CC BY 4.0