Edit $ENV:Path in Powershell

Edit the System PATH statement globally in Windows Powershell with 4 new Advanced functions. Add-path, Set-path, Remove-path, Get-path - SUPPORTS -whatif parameter

 
 
 
 
 
2.9 Star
(8)
Add to favorites
Operating System
2/20/2015
E-mail Twitter del.icio.us Digg Facebook
  • can't import the module
    1 Posts | Last post July 10, 2019
    • need help, when i try to import the module, it gives error
      
      PS C:\Windows> Import-Module Env-Path
      At C:\WINDOWS\system32\WindowsPowerShell\v1.0\Modules\Env-Path\Env-Path.psm1:38 char:1
      + {
      + ~
      Missing closing '}' in statement block or type definition.
      + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
      + FullyQualifiedErrorId : MissingEndCurlyBrace
      
      Import-Module : The specified module 'Env-Path' was not loaded because no valid module file was found in any module directory.
      At line:1 char:1
      + Import-Module Env-Path
      + ~~~~~~~~~~~~~~~~~~~~~~
      + CategoryInfo          : ResourceUnavailable: (Env-Path:String) [Import-Module], FileNotFoundException
      + FullyQualifiedErrorId : Modules_ModuleNotFound,Microsoft.PowerShell.Commands.ImportModuleCommand
  • Add another Environment Variable to the Path
    1 Posts | Last post December 06, 2017
    • So far, I am having good success adding and removing if I use only hard coded paths. 
      
      Although, if I try to add %JAVA_HOME% in there, the "check path" fails due to not expanding the existing variable. 
      
      Can this be solved by adding -ExpandProperty to only check the actual variable path? Yet, when all said and done, add the variable instead of the path?
      
      Thanks
  • How to install the Module
    2 Posts | Last post September 27, 2017
    • HI I was wondering there is a way that I Can install this module and don't have to import-module everytime I start a new session ?.
      Thanks in Advance
    • I sort it out adding the module to the powershell_profile
  • script is not running oder win 7 sp1
    3 Posts | Last post November 01, 2016
    • Hello Sean,
      
      this script is not running under Win 7. No funktion.
      I start this script with path.ps1 ADD-PATH C:\test and nothing is happend
      
      Can you check this script please
      
      thx
    • Again, on how to use functions in powershell:
      http://www.jonathanmedd.net/2015/01/how-to-make-use-of-functions-in-powershell.html
      
      using the modules method:
      https://www.simple-talk.com/sysadmin/powershell/an-introduction-to-powershell-modules/
      
      I stored this under the name: %UserProfile%\Documents\WindowsPowerShell\ModulesPathTools\PathTools.psm1
      
      After executing this in powershell:
      Import-Module PathTools -PassThru
      
      I get this as a result:
      
      ModuleType Version    Name                                ExportedCommands
      ---------- -------    ----                                ----------------
      Script     0.0        PathTools
      
      Checking if things work:
      Get-Command -Module PathTools
      
      Gives this:
      
      CommandType     Name                                               Version    Source
      -----------     ----                                               -------    ------
      Function        ADD-PATH                                           0.0        PathTools
      Function        GET-PATH                                           0.0        PathTools
      Function        REMOVE-PATH                                        0.0        PathTools
      Function        SET-PATH                                           0.0        PathTools
      Function        TEST-LocalAdmin                                    0.0        PathTools
      
      Now finally I think I can do:
      PS Y:\> ADD-PATH C:\Temp\PATH-TEST
      Need to RUN AS ADMINISTRATOR first
      1
      
      So I start PowerShell as an admin and this command indeed adds C:\Temp\PATH-TEST to the path variable. Still with the error message, that is something I might work out next.
      
      
    • This is on Windows 7 SP1 64bit BTW. Works fine.
  • The script just doesnt work?!
    2 Posts | Last post November 01, 2016
    • On paths with spaces.
      
      Doesnt work on Windows 7.
      Test-LocalAdmin function does not exist.
      
      Poor documentation.
      
      Seems like a half hearted effort really. Disappointing.
    • On how to handlefunctions in powershell:
      http://www.jonathanmedd.net/2015/01/how-to-make-use-of-functions-in-powershell.html
      
      The function "TEST-LocalAdmin" is defined right at the top of the code block.
  • Discount partial matches on REMOVE-PATH()
    2 Posts | Last post May 30, 2016
    • in REMOVE-PATH()  you need to replace this:
      $Verify=$newpath.split(';') -contains $RemovedFolder 
      
      with this:
      
      $var  =  $myarray | %{$_} | where-object { $_ -eq  $RemovedFolder } | select $_
      $Verify= -not [String]::IsNullOrEmpty($var)  
      
      
      otherwise you get partial matches
    • replace $myarray with $newpath.split(';')
  • Double check if the path already exists.
    2 Posts | Last post May 23, 2015
    • dod
      It is more a suggestion. When you add the path and you didn't reboot the server and add the same path again it will add up path to the registry string. To prevent this scenario i added following to add-path function:
      
      IF (($OldPath | Select-String -SimpleMatch $AddedFolder)  -and (!($ENV:PATH | Select-String -SimpleMatch $AddedFolder)))
      { Return ‘Folder already within Path registry string but is not in the environment, you might have to reboot the server' }
      
    • Thanks for sharing how you addressed this dod.
      I noticed that there is a call of a function named TEST-PATH, which is not defined in this module. Using your 2 line replacement covered that missing piece, as well as allowed me to remove the 7 lines of code that followed.
      
      Another win for the PowerShell community, working together! #PSFTW
  • Few things needd to be fixed..
    3 Posts | Last post February 20, 2015
    • 1. TEST-LocalAdmin is not defined
      
      2. ADD-PATH gives error (below) at following line, but still adds the folder to PATH:
         If (!($AddedFolder[-1] -match '\')) { $Newpath=$Newpath+'\'} 
      
      3. REMOVE-PATH gives error at following line:
         If (!($RemovedFolder[-2] -match '\')) { $RemovedFolder=$RemovedFolder -replace ";","\;" }
      
      4. REMOVE-PATH does not remove the folder (after a quick fix to escape the match argument in item 3 above).
      
      5. I am just trying to understand how does -1 work on a string array. I came from a C# background, so this seems like an exception to me, but somehow it works in powershell.
      
      
      Error:
      
      parsing "\" - Illegal \ at end of pattern.
      At C:\PowerShell\EditSystemPath.ps1:47 char:5
      + If (!($AddedFolder[-1] -match '\')) { $Newpath=$Newpath+'\'}
      +     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
          + CategoryInfo          : OperationStopped: (:) [], ArgumentException
          + FullyQualifiedErrorId : System.ArgumentException
    • Regarding 5, need to type "Get-Help about_Arrays" in powershell, and it gives following information:
      
      Negative numbers count from the end of the array. For example, "-1" refers to the last element of the array.
      
      
    • Thanks for the feedback.   Your input has been added to the original script.  I've also made some changes with the method used to identify the Path to get more exact matches
      
      Thanks!
      Sean