Expanding environmental variables using vbs

12/14/07

Permalink 10:37:06 am, by dave Email , 190 words, 91 views   English (US)
Categories: Windows Scripting, Wsh Scripts

Expanding environmental variables using vbs

Recently I was messing around with some registry enumeration scripts to show the paths of executables that were being referenced in the registry. To do this properly, I needed to expand the system variables that were stored in the registry. I figured that there might be some other folks that needed to do the same thing, so here is a little function that will handle it for you.

[More:]

function expandEnvString( envVal )

dim wshShell

set wshShell = createobject("WScript.Shell")

if isnull( envVal ) or envVal = "" then
    expandEnvString = ""
    exit function
end if

expandEnvString = wshShell.expandenvironmentstrings( envVal )

set wshShell = nothing

end function

To use the function, you call it passing in the environmental variable you want expanded. Something like......

wscript.echo expandEnvString( "%WINDIR%" )

This will give you results something like......

C:\>cscript funcTest.vbs
Microsoft (R) Windows Script Host Version 5.6
Copyright (C) Microsoft Corporation 1996-2001. All rights reserved.

C:\WINDOWS

I hope this post helped you out. If it didn't, I am always looking for new scripts to add so submit a request for your question or need and I will see if I can answer it.



Did you like this post? If so, Share it!  del.icio.us digg reddit slashdot this article Facebook Twitter MySpace Email



Pingbacks:

No Pingbacks for this post yet...

Scripts

This is somewhere I can post interesting snippets as I come across them. Hopefully some folks out there will find this helpful.

Search

Follow Me:

Misc

Who's Online?

  • Guest Users: 1

powered by b2evolution free blog software