Home > Powershell > Powershell: Adding Directories to Path Statement

Powershell: Adding Directories to Path Statement

Adding directories to the Path statement is a rarity for most techs these days but on occasion, such as configuring Sharpeoint 2010 to use an Adobe IFilter or my own desire to make it easier to run powershell scripts, updating the Path statement must be done.  Either way it’s just the reason I was looking for to write another powershell script. 🙂

I always approach writing powershell code with the intention of making it resuable, mostly in the form of a function.  The function I’ve created this time is called  AddTo-SystemPath

I begin by defining parameters.  Since there may be a need to add several directories to the path statement I’ve cast the $PathToAdd variable  as an array.

  Param(
     [array]$PathToAdd
 )

A Foreach loop will then be run against the $PathToAdd variable .  It also seemed like best practice  to make sure that the Path statement didn’t already contain the directory(s) in the $PathToAdd variable so comparison is used inside an If\Else statement. The  $VerifiedPathsToAdd variable is then populated with the directories to add.

 Foreach($Path in $PathToAdd) {            

    #Verify if the Path statement already contains the folder
    if($env:Path -like "*$Path*") {
       Write-Host "$Path already exists in Path statement" }
    else { $VerifiedPathsToAdd += ";$Path"
       Write-Host "`$VerifiedPathsToAdd updated to contain: $Path"}

I now want to make sure that $VerifiedPathsToAdd contains something, and if so update the Path statement  using the [Environment] class.

The code below containing  [Environment]::SetEnvironmentVariable() is too long to display as one line so I’ve divided it into the class and method overloads.

[Environment]::SetEnvironmentVariable
("Path",$env:Path + $VerifiedPathsToAdd,"Process")

It’s possible to update the Path statement using just the $Env:Path variable, however it’s not persistent and any added values will be lost when the PS session closes. An example of using this non-persistent method is:

$ENV:Path = $ENV:Path + ";$Path"

The complete If statement containing the persistent method is:

  #Verify that there is something in $VerifiedPathsToAdd to update the Path statement
  if($VerifiedPathsToAdd -ne $null) {
    Write-Host "`$VerifiedPathsToAdd contains: $verifiedPathsToAdd"
    Write-Host "Adding $Path to Path statement now..."
  [Environment]::SetEnvironmentVariable("Path",$env:Path + $VerifiedPathsToAdd,"Process")
   }#End If

The complete Function is below:

Function AddTo-SystemPath {            

 Param(
  [array]$PathToAdd
  )
 $VerifiedPathsToAdd = $Null
 Foreach($Path in $PathToAdd) {            

  if($env:Path -like "*$Path*") {
   Write-Host "Currnet item in path is: $Path"
   Write-Host "$Path already exists in Path statement" }
   else { $VerifiedPathsToAdd += ";$Path"
    Write-Host "`$VerifiedPathsToAdd updated to contain: $Path"}            

  if($VerifiedPathsToAdd -ne $null) {
   Write-Host "`$VerifiedPathsToAdd contains: $verifiedPathsToAdd"
   Write-Host "Adding $Path to Path statement now..."
   [Environment]::SetEnvironmentVariable("Path",$env:Path + $VerifiedPathsToAdd,"Process")            

   }
  }
 }
Advertisements
Categories: Powershell Tags:
  1. Stephen Mills
    February 11, 2012 at 3:50 am

    Actually in some cases your script won’t add the path to the variable because it will think it is already in the path. For example: If you already have a path that includes “C:\Windows\System32” and you want to add “C:\Windows”, it will think it is already in the path since “C:\Windows\System32” -like “*C:\Windows*’ evaluates to true. To evaluate it correctly you really have to split the current path on “;” and then remove any trailing “\” from the paths. You then need to do the same to the paths being passed in and then you can do a contains expression to see if it is already there.

    This will split the current path and remove trailing “\”.
    $env:path -Split ‘;’ -replace ‘\\+$’, ”

    The other thing that is a pain to look for is short file names. For Example: C:\PROGRA~1 is the same as “C:\Program Files”. Although it is possible to check for this it is a pain and not really worth it in most cases. You could do this using Get-Item for each path in the current $ENV:Path.

    Here’s a version with the changes I suggested above, except for the short path thing.

    Function AddTo-SystemPath
    {
    Param(
    [array]$PathToAdd
    )
    $VerifiedPathsToAdd = $Null
    $PathArray = $Env:path -Split ‘;’ -replace ‘\\+$’, ”
    Foreach($Path in ( $PathToAdd | % { $_.TrimEnd(‘\’) } ) )
    {

    if($PathArray -contains $Path )
    {
    Write-Host “Currnet item in path is: $Path”
    Write-Host “$Path already exists in Path statement”
    }
    else
    {
    $VerifiedPathsToAdd += “;$Path”
    Write-Host “`$VerifiedPathsToAdd updated to contain: $Path”
    }

    if($VerifiedPathsToAdd -ne $null)
    {
    Write-Host “`$VerifiedPathsToAdd contains: $verifiedPathsToAdd”
    Write-Host “Adding $Path to Path statement now…”
    [Environment]::SetEnvironmentVariable(“Path”,$env:Path + $VerifiedPathsToAdd,”Process”)
    }
    }
    }

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: