Home > Software engineering >  Reference default path in powershell script
Reference default path in powershell script

Time:05-01

Is there a way to call the system path if the path has been changed in the current terminal? i.e.:

$env:Path = "C:\some new path"   
#some coding that requires a different path set up
$env:Path = $defaultPath #would have to define $defaultPath by calling the system default path

CodePudding user response:

It's still stored in the registry so you can just query it:

  • Located: HKLM:\System\CurrentControlSet\Control\Session Manager\Environment
$key = "HKLM:\System\CurrentControlSet\Control\Session Manager\Environment"
(Get-ItemProperty -Path $key -Name PATH).Path

Querying the key using Get-ItemProperty will give you just the property's value.

CodePudding user response:

Thanks for the response about $key, that worked! Now if someone could help me figure out why my if statements aren't working, I'd appreciate it.

function anaconda { 
    $key = "HKLM:\System\CurrentControlSet\Control\Session Manager\Environment"
    param(    
    [Alias('d')]
    [Parameter(ParameterSetName='Deactivate')]
    [switch] $Deactivate
    ,
    [Alias('a')]
    [Parameter(ParameterSetName='Activate')]
    [switch] $Activate
    )

    if ($Activate) {
        & 'C:\Anaconda3\shell\condabin\conda-hook.ps1' ; conda activate 'C:\Anaconda3'
    } 
#starts conda environment

    elseif ($Deactivate) 
    {
        & 'C:\Anaconda3\shell\condabin\conda-hook.ps1' ; conda deactivate 'C:\Anaconda3'
        $env:path = (Get-ItemProperty -Path $key -Name PATH).Path
    } #deactivates conda and resets path

}

  • Related