Add the BIN folder to the path environment variable with powershell

I often go to execute a stsadm command and find it is not in the path on the server I am working on, so I looked for a while of a way of adding it permanently through powershell and then realised it's a straightforward task with .NET code. The following will add the BIN folder within the program files structure to the PATH environment variable at local machine level so that all users will benefit.

   1:  $envpath = [environment]::GetEnvironmentVariable("Path","Machine")
   2:  $binpath = $env:Programfiles + "\Common Files\Microsoft Shared\web server extensions\12\BIN"
   3:  if ($envpath.contains($binpath) -ne $true ) {
   4:  $envpath = $envpath + ";" + $binpath
   5:  [environment]::SetEnvironmentVariable("Path",$envpath,"Machine")
   6:  Write-output "BIN Path added."
   7:  }
   8:  else
   9:  {
  10:  Write-output "BIN Path already added."
  11:  }

I have added a check to not add it if it is already present as the traditional method of using

SET PATH=%PATH%;C:\Program files\Common Files\Microsoft Shared\Web Server Extensions\12\BIN

ends up with multiple copies of the BIN path in the variable and only applies for the current session.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

 

Dilbert of the day