Azure: Claim VM space by moving the temporary folders to the D drive

We have a bunch of Workstations Virtual Machines hosted in our Azure development environment that have been hogging a lot of space. I had a quick look into those and I noticed that a lot of space is being used by temporary folders, both by the system and by the user profiles. I came up with a simple solution to claim most of this space, which I would like to share with you: Moving temporary folders to the D: drive (known as the temporary drive).

When you create a virtual machine (VM) in Azure, it automatically provisions a temporary storage drive (you can read more about it here). Windows VMs in Azure are configured by default to use this drive for holding virtual memory file (pagefile.sys), but beyond that, nothing much is done with this drive. So I took the initiative to move the temporary folders onto this drive. Here is how I did it.

Open Control Panel, then open the System applet (which is under System and Security). On the System window, click on Advanced System Settings which is listed at the left-side pane in order to open the System Properties window. At the System Properties dialog, clicked on the Advanced tab, followed the Environment Variables button at the bottom.

The first temporary directory to be moved is the system-wide one, which is located under C:\Windows\Temp by default. Under System Variables, look for two entries: TMP and TEMP. To edit an entry you can either double-click on it, or select it then click the Edit button. Change the variable value for both entries to D:\ (i.e. the root of the temporary drive).

This temporary directory is used for some system processes, but it is not the one that tends to hog most space. We now need to look at the user profile temporary directories. Still at the Environment Variables window, you can see that there are also a TMP and a TEMP entry listed under User Variables for your username. Modify the variable value of each entry to D:\%USERPROFILE%\Temp

The idea is to hold the temporary files of your user profile under a directory in the D: drive which is named after your user. This is important since mixing temporary files among different users and the system account can be a bad idea. However, it is quite likely that this directory does not exist in the D: drive. So we need to create it. In order to do so, open a command prompt window and type the following at the prompt: md D:\%USERPROFILE%

The above code will create the directory under the D: drive. However, since the D: drive can be wiped-off, we cannot guarantee that this directory will always exist. So how do we circumvent this issue? What I did was to create a script filed called createTemp.bat with the above line of code, save it somewhere on my C: drive (e.g. C:\Scripts) and then have this file called every time a user logs in. In order to do that, you can either add an entry to the file in the registry under HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run, or you can add a shortcut to it under the Statup. Make sure you add it under the folder for all users though, which is located under C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup.

As a final note, please bear in mind that this approach might not be supported by Microsoft. While I feel quite comfortable performing this change in our development environment, particularly on development workstations, I would think twice before doing it the same for mission-critical hosts in our production environment.

Leave a comment