Powershell, environment variables issue

• Question

• User692426287 posted
Greetings everyone...

So I'm writing a script that involves capturing the IIS Logs Directory. To find the directory of the where IIS logs are located at, I do the following: \$IISLogDirectory = Get-WebConfigurationProperty "/system.applicationHost/sites/siteDefaults" -name logfile.directory.value

I get the following output:
%SystemDrive%\inetpub\IISLogFiles

This is correct and is exactly what I'm looking for. The only problem is Powershell doesn't know how to interpret the %systemdrive% environment valuable due to the percent signs. When I do Get-ChildItem -path \$IISLogDirectory, I get an error because of the percent signs in the string.

I suppose I can add a command to strip the percent signs, but I was wondering if there was some sort of syntax I could use so that powershell interprets the percent signs correctly. How do you folks handle percent signs in environment variables?
Wednesday, July 27, 2011 11:35 AM

• User-176674611 posted

Hi,

Sorry for the late reply. As far as I can think of, the easiest way is to use an -replace operation to replace '%SystemDrive%' with 'C:'. So scripts will look like:

\$IISLogDirectory = Get-WebConfigurationProperty "/system.applicationHost/sites/siteDefaults" -name logfile.directory.value
\$IISLogDirectory = \$IISLogDirectory -replace "%SystemDrive%", "C:"
Get-ChildItem -path \$IISLogDirectory

Hope this helps, thanks.

• Marked as answer by Tuesday, September 28, 2021 12:00 AM
Monday, August 1, 2011 11:17 PM

All replies

• User-176674611 posted

Hi,

Sorry for the late reply. As far as I can think of, the easiest way is to use an -replace operation to replace '%SystemDrive%' with 'C:'. So scripts will look like:

\$IISLogDirectory = Get-WebConfigurationProperty "/system.applicationHost/sites/siteDefaults" -name logfile.directory.value
\$IISLogDirectory = \$IISLogDirectory -replace "%SystemDrive%", "C:"
Get-ChildItem -path \$IISLogDirectory

Hope this helps, thanks.

• Marked as answer by Tuesday, September 28, 2021 12:00 AM
Monday, August 1, 2011 11:17 PM
• User1065545921 posted

Try:

\$IISLogDirectory -replace "%SystemDrive%",\$env:SystemDrive

Tuesday, June 18, 2013 10:28 PM
• User-1011574161 posted

I know this is an old post, but just in case this helps somone else...

Even better is to use:

[System.Environment]::ExpandEnvironmentVariables(<string>)

For example:

[System.Environment]::ExpandEnvironmentVariables((gi 'IIS://sites/default web site').logfile.directory)

Friday, November 15, 2013 5:39 PM