PowerShell and MS-DOS variables

The first question that comes to mind after reading the title is "What do MS-DOS variables have to do with PowerShell???".

Well, although you may be working solely with PowerShell - just like myself - you will eventually come across a variable in a configuration file, registry key etc. that is going to contain an MS-DOS formatted variable.

Let's take the physical path of an IIS web site for example:

PS C:\> Get-Website -Name "Default Web Site" | % physicalpath
%SystemDrive%\inetpub\wwwroot

PS C:\>

In order for this value to be usable within PowerShell, we'll probably need to expand the "SystemDrive" variable.

Fortunately, .NET has the appropriate tool for us, the "ExpandEnvironmentVariables" function.

Invoking the above function with a string that contains a variable will return a new string with the variable extended.

We are going to use the website's physical path once more and this time we'll expand the result:

PS C:\> $path = Get-Website -Name "Default Web Site" | % physicalpath

PS C:\> [System.Environment]::ExpandEnvironmentVariables($path)
C:\inetpub\wwwroot

PS C:\>

Have fun!

Popular posts from this blog

Domain Controller Machine Password Reset

Configuring a Certificate on Exchange Receive Connector

Running Multiple NGINX Ingress Controllers in AKS