次の方法で共有


Setting up a Visual Studio 2008 PowerShell Prompt

I finally got some time to setup a powershell script that mimics the vcvars32.bat file that the Visual Studio 2008 command prompt loads (if you're on a 32-bit machine) and sets up all the environment variables for using the development command-line tools.

To create your own VS 2008 PowerShell Prompt just create a text file and call it "vcvars32.ps1" and add the following content:

 Write-Host "Setting Powershell environment for using Microsoft Visual Studio 2008 x86 tools."
$Env:VSINSTALLDIR="C:\Program Files\Microsoft Visual Studio 9.0"
$Env:VCINSTALLDIR="C:\Program Files\Microsoft Visual Studio 9.0\VC"
$Env:FrameworkDir="C:\Windows\Microsoft.NET\Framework"
$Env:FrameworkVersion="v2.0.50727"
$Env:Framework35Version="v3.5"

$WinSdkInstallFolder = (get-itemproperty -path "HKLM:\SOFTWARE\Microsoft\Microsoft SDKs\Windows" -name CurrentInstallFolder).CurrentInstallFolder

if ($WinSdkInstallFolder -ne "")
{
    $Env:WinSdk = $WinSdkInstallFolder
    $Env:Path = $WinSdkInstallFolder + "bin;" + $Env:Path
    $Env:INCLUDE = $WinSdkInstallFolder + "include;" + $Env:INCLUDE
    $Env:LIB = $WinSdkInstallFolder + "lib;" + $Env:LIB
}

$Env:Path="C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE;C:\Program Files\Microsoft Visual Studio 9.0\VC\BIN;C:\Program Files\Microsoft Visual Studio 9.0\Common7\Tools;C:\Windows\Microsoft.NET\Framework\v3.5;C:\Windows\Microsoft.NET\Framework\v2.0.50727;C:\Program Files\Microsoft Visual Studio 9.0\VC\VCPackages;" + $Env:Path
$Env:INCLUDE = "INCLUDE=C:\Program Files\Microsoft Visual Studio 9.0\VC\ATLMFC\INCLUDE;C:\Program Files\Microsoft Visual Studio 9.0\VC\INCLUDE;" + $Env:INCLUDE
$Env:LIB = "C:\Program Files\Microsoft Visual Studio 9.0\VC\ATLMFC\LIB;C:\Program Files\Microsoft Visual Studio 9.0\VC\LIB;" + $Env:LIB
$Env:LIBPATH = "C:\Windows\Microsoft.NET\Framework\v3.5;C:\Windows\Microsoft.NET\Framework\v2.0.50727;C:\Program Files\Microsoft Visual Studio 9.0\VC\ATLMFC\LIB;C:\Program Files\Microsoft Visual Studio 9.0\VC\LIB;" + $Env:LIBPATH

To be able to execute scripts from a powershell prompt you need to set the correct execution policy for your environment. The default execution policy is "Restricted" which does not allow scripts to be run.

You can choose to set the execution policy to allow signed scripts to be run or to disable it altogether (usual disclaimer: This option will allow untrusted scripts to be run, so make sure you know the implications and choose wisely...)

To set the execution policy you can use the command:

Set-ExecutionPolicy -executionpolicy <policy>

To sign the script, you must go through the steps described by the following article:

Get-Help about_signing

All that's left is to create a shortcut that runs your "vcvars32.ps1" script at startup. So, just create a new shortcut and set the following Target property:

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -NoLogo -NoExit -File <path to your "vcvars32.ps1" file>

PowerShell is a great tool for developers. Hope this helps you take advantage of it.

Comments