PowerShell How-To: Emulating Cmd.exe's Set Command

Give this custom PowerShell function a try

Bill Stewart

December 12, 2011

3 Min Read
ITPro Today logo in a gray background | ITPro Today

Downloads141086.zip

 

 

 

 

I'm a long-time user of the command shell (Cmd.exe) in Windows. Old habits die hard, so I continually find myself typing the Set command in the Windows PowerShell console when I want to list, set, or clear environment variables. In PowerShell, Set is an alias for the Set-Variable cmdlet, but it doesn't work with environment variables. Instead, you have to use the Get-ChildItem, Get-Item, or Remove-Item cmdlet with the ENV: drive.

Rather than trying to break the habit, I decided to accommodate it by writing my own PowerShell Set function. I wanted my PowerShell Set function to behave like Cmd.exe's Set command.

To get this behavior, my PowerShell Set function uses the MyInvocation object's InvocationName and Line properties to capture information about the function's command line. The InvocationName property contains the name of the command. The Line property returns the line used to invoke the command. The custom Set function is shown in Listing 1.

You can't use the Set function as part of a PowerShell expression, such as

 

(Set processor_level).GetType() 

 

The reason for this limitation is that the Set function needs to use a slightly unorthodox command-line parsing technique because the Line property returns the entire line, not just the expression. As a result, in this case, the Environment variable not found error is thrown.

 

However, the Set function has two advantages over Cmd.exe's Set command. First, it outputs DictionaryEntry objects, just like when you use the command

 

Get-ChildItem ENV: 

 

Second, the Set function uses wildcard matching. For example, the command

 

 

Set P 

 

matches only a variable named P, whereas in Cmd.exe this command will list all variables that start with P. To do this with the Set function, you'd use

 

 

Set P* 

 

instead. This is equivalent to the PowerShell command

 

 

Get-ChildItem ENV:p* |Sort-Object Name 

 

You can download the Set function's code (Set.ps1) by going to www.windowsitpro.com, entering 141086 in the Search box, and clicking the 141086.zip hotlink. To make the Set function a permanent part of your PowerShell command-line experience, put the code in your PowerShell profile. If you aren't familiar with PowerShell profiles, type the command

 

Get-Help about_Profiles | More

at a PowerShell prompt.

 

 

 

Listing 1: The Set Function

 

# If an alias exists, remove it.

If (Test-Path ALIAS:set) { Remove-Item ALIAS:set }

 

Function Set {

  If (-Not $ARGS) {

    Get-ChildItem ENV: | Sort-Object Name

    Return

  }

  $myLine = $MYINVOCATION.Line

  $myName = $MYINVOCATION.InvocationName

  $myArgs = $myLine.Substring($myLine.IndexOf($myName) + $myName.Length + 1)

  $equalPos = $myArgs.IndexOf("=")

  # If the "=" character isn’t found, output the variables.

  If ($equalPos -eq -1) {

    $result = Get-ChildItem ENV: | Where-Object { $_.Name -like "$myArgs" } |

      Sort-Object Name

    If ($result) { $result } Else { Throw "Environment variable not found" }

  }

  # If the "=" character is found before the end of the string, set the variable.

  ElseIf ($equalPos -lt $myArgs.Length - 1) {

    $varName = $myArgs.Substring(0, $equalPos)

    $varData = $myArgs.Substring($equalPos + 1)

    Set-Item ENV:$varName $varData

  }

  # If the "=" character is found at the end of the string, remove the variable.

  Else {

    $varName = $myArgs.Substring(0, $equalPos)

    If (Test-Path ENV:$varName) { Remove-Item ENV:$varName }

  }

}

 

Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like