Customing Scripts and Batch Files with Environment Variables

One of the most popular uses of environment variables is from within batch files. For example, you could add the following commands to an existing logon script to provide users with basic information:

@echo Thank you for logging on %username%.
@echo The current time is %time% on %date%.
@echo Your user profile is located in %userprofile%.
@echo Your home directory is located at %homedrive%
pause

By the same token, information provided by variables could be used within a script to make decisions. For example, an administrator might choose to copy a file to a system if the %os% variable equals Windows_NT, using an IF statement.

If scripting isn’t your thing, you can still manipulate variables to serve your needs. For example, you could create your own new user variable called HIDDEN that points to a folder that you have hidden from view with the file system, and then use the %hidden% variable when you want to access that folder quickly. Similarly, you could also change the path of temporary file locations (the TMP and TEMP) variables such that all temporary files are saved to a single folder named D:\tempfolder, thus making it easier to delete or find temporary files when necessary.

Author: Dan DiNicolo

Dan DiNicolo is a freelance author, consultant, trainer, and the managing editor of 2000Trainers.com. He is the author of the CCNA Study Guide found on this site, as well as many books including the PC Magazine titles Windows XP Security Solutions and Windows Vista Security Solutions. Click here to contact Dan.