A simple Powershell function to import an environment

A couple of days ago, I was working on a powershell-based script for mstest automation and I needed to call vsdevcmd.bat from Visual Studio’s tools folder.

function Invoke-Environment([Parameter(Mandatory=1)][string]$Command, [switch]$Output, [switch]$Force)
{
    $stream = if ($Output) { ($temp = [IO.Path]::GetTempFileName()) } else { 'nul' }
    $operator = if ($Force) {'&'} else {'&&'}
    
    foreach($_ in cmd /c " $Command > `"$stream`" 2>&1 $operator SET")
    {
        if ($_ -match '^([^=]+)=(.*)')
        {
            [System.Environment]::SetEnvironmentVariable($matches[1], $matches[2])
        }
    }
    
    if ($Output)
    {
        Get-Content -LiteralPath $temp
        Remove-Item -LiteralPath $temp
    }
}

To use the function:

Invoke-Environment '"C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\Tools\VsDevCmd.bat"'
---
If you have any questions or feedback, please reach out @flowerinthenyt.