Export (0) Print
Expand All
Expand Minimize

Converting the Windows Script Host ExpandEnvironmentStrings Method


Windows PowerShell


Definition: Returns an environment variable's expanded value.

ExpandEnvironmentStrings


Environment variables often contain useful information to script writers; for example, the environment variable windir (%windir%) reports the location of the Windows folder on a computer. Unfortunately, there’s a problem with environment variables: it can be difficult, at times, to tease out the actual value (e.g., the actual location of the Windows folder). That’s the reason for Windows Script Host’s ExpandEnvironmentStrings method: pass ExpandEnvironmentStrings the name of an environment variable, and the method will report back the value of that variable.

In Windows PowerShell there are at least two ways to retrieve the value of an environment variable. For one, you can use the .NET Framework and the GetEnvironmentVariable method to return this information. For example, this command returns the value of the environment variable windir, storing that information in $a:


$a = [environment]::GetEnvironmentVariable("windir")


In turn, $a will now contain the path to the Windows folder:


C:\Windows


Alternatively, you can use the Get-Item cmdlet to connect to the Env:\ drive and the windir environment variable, and then retrieve just the Value property. That command looks like this:


$a = (Get-Item Env:\windir).Value



See conversions of other Windows Script Host methods and properties.
Return to the VBScript to Windows PowerShell home page

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft