Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 828 Vote(s) - 3.48 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Setting Windows PowerShell environment variables

#11
You can also modify user/system environment variables **permanently** (i.e. will be persistent across shell restarts) with the following:

Modify a system environment variable

[Environment]::SetEnvironmentVariable
("Path", $env:Path, [System.EnvironmentVariableTarget]::Machine)

Modify a user environment variable

[Environment]::SetEnvironmentVariable
("INCLUDE", $env:INCLUDE, [System.EnvironmentVariableTarget]::User)

Usage from comments - add to the system environment variable

[Environment]::SetEnvironmentVariable(
"Path",
[Environment]::GetEnvironmentVariable("Path", [EnvironmentVariableTarget]::Machine) + ";C:\bin",
[EnvironmentVariableTarget]::Machine)

String based solution is also possible if you don't want to write types

[Environment]::SetEnvironmentVariable("Path", $env:Path + ";C:\bin", "Machine")
Reply

#12
My suggestion is this one:

I have tested this to add `C:\oracle\x64\bin` to environment variable `Path` permanently and this works fine.

$ENV:PATH

The first way is simply to do:

$ENV:PATH=”$ENV:PATH;c:\path\to\folder”

But this change isn’t permanent. `$env:path` will default back to what it was before as soon as you close your PowerShell terminal and reopen it again. That’s because you have applied the change at the session level and not at the source level (which is the registry level). To view the global value of `$env:path`, do:

Get-ItemProperty -Path ‘Registry::HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment’ -Name PATH

Or more specifically:

(Get-ItemProperty -Path ‘Registry::HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment’ -Name PATH).path

Now to change this, first we capture the original path that needs to be modified:

$oldpath = (Get-ItemProperty -Path ‘Registry::HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment’ -Name PATH).path

Now we define what the new path should look like. In this case we are appending a new folder:

$newpath = “$oldpath;c:\path\to\folder”

Note: Be sure that the `$newpath` looks how you want it to look. If not, then you could damage your OS.

Now apply the new value:

Set-ItemProperty -Path ‘Registry::HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment’ -Name PATH -Value $newPath

Now do one final check that it looks like how you expect it to:

(Get-ItemProperty -Path ‘Registry::HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment’ -Name PATH).Path

You can now restart your PowerShell terminal (or even reboot the machine) and see that it doesn’t rollback to its old value again.

Note the ordering of the paths may change so that it’s in alphabetical order, so make sure you check the whole line. To make it easier, you can split the output into rows by using the semi-colon as a delimiter:

($env:path).split(“;”)

Reply

#13
Within PowerShell, one can navigate to the environment variable directory by typing:

Set-Location Env:

This will bring you to the Env:\> directory. From within this directory:

To see all environment variables, type:

Env:\> Get-ChildItem

To see a specific environment variable, type:

Env:\> $Env:<variable name>, e.g. $Env:Path

To set an environment variable, type:

Env:\> $Env:<variable name> = "<new-value>", e.g. $Env:Path="C:\Users\"

To remove an environment variable, type:

Env:\> remove-item Env:<variable name>, e.g. remove-item Env:SECRET_KEY

More information is in *[About Environment Variables][1]*.

[1]:

[To see links please register here]




Reply

#14
Only the answers that push the value into the registry affect a *permanent* change (so the majority of answers on this thread, including the accepted answer, do *not* permanently affect the `Path`).

The following function works for both `Path` / `PSModulePath` and for `User` / `System` types. It will also add the new path to the current session by default.

function AddTo-Path {
param (
[string]$PathToAdd,
[Parameter(Mandatory=$true)][ValidateSet('System','User')][string]$UserType,
[Parameter(Mandatory=$true)][ValidateSet('Path','PSModulePath')][string]$PathType
)

# AddTo-Path "C:\XXX" "PSModulePath" 'System'
if ($UserType -eq "System" ) { $RegPropertyLocation = 'HKLM:\System\CurrentControlSet\Control\Session Manager\Environment' }
if ($UserType -eq "User" ) { $RegPropertyLocation = 'HKCU:\Environment' } # also note: Registry::HKEY_LOCAL_MACHINE\ format
$PathOld = (Get-ItemProperty -Path $RegPropertyLocation -Name $PathType).$PathType
"`n$UserType $PathType Before:`n$PathOld`n"
$PathArray = $PathOld -Split ";" -replace "\\+$", ""
if ($PathArray -notcontains $PathToAdd) {
"$UserType $PathType Now:" # ; sleep -Milliseconds 100 # Might need pause to prevent text being after Path output(!)
$PathNew = "$PathOld;$PathToAdd"
Set-ItemProperty -Path $RegPropertyLocation -Name $PathType -Value $PathNew
Get-ItemProperty -Path $RegPropertyLocation -Name $PathType | select -ExpandProperty $PathType
if ($PathType -eq "Path") { $env:Path += ";$PathToAdd" } # Add to Path also for this current session
if ($PathType -eq "PSModulePath") { $env:PSModulePath += ";$PathToAdd" } # Add to PSModulePath also for this current session
"`n$PathToAdd has been added to the $UserType $PathType"
}
else {
"'$PathToAdd' is already in the $UserType $PathType. Nothing to do."
}
}

# Add "C:\XXX" to User Path (but only if not already present)
AddTo-Path "C:\XXX" "User" "Path"

# Just show the current status by putting an empty path
AddTo-Path "" "User" "Path"

Reply

#15
To be clear, the 1990's Windows way of click on **Start**, right click on **This PC**, and choose **Properties**, and then select **Advanced system settings**, and then in the dialog box that pops up, select **Environment Variables**, and in the list double clicking on **PATH** and then using the **New**, **Edit**, **Move Up** and **Move Down** all still work for changing the PATH. Power shell, and the rest of Windows get whatever you set here.

Yes you can use these new methods, but the old one still works. And at the base level all of the permanent change methods are controlled ways of editing your registry files.

Reply

#16
Lots of examples of appending, or overwriting. Here is an example of prepending a path on powershell for Linux, Ubuntu 18.04 with `pwsh` 7.1.3

$ENV:PATH = "/home/linuxbrew/.linuxbrew/bin:$ENV:PATH"

I'm specifically adding the linuxbrew (homebrew for linux) bin directory to take precedence over the system installed. It helped solve an issue I was having and although this was the most helpful place, it also left me "experimenting".

Note that the `:` is Linux path separator, whereas on Windows (or at least my windows) you would use `;` for powershell typically.
Reply

#17
WARNING: save a copy of your existing path by doing `$env:path >> a.out` in a PowerShell prompt, in case something goes wrong.

From the PowerShell prompt:

setx PATH "$env:path;\the\directory\to\add" -m

You should then see the text:

SUCCESS: Specified value was saved.

Restart your session, and the variable will be available. `setx` can also be used to set arbitrary variables. Type `setx /?` at the prompt for documentation.


Reply

#18
Editing the registry key in [@ali Darabi's answer][1] worked best for me, but
When I didn't have the right permissions to do it from Powershell. So I edited it directly in regedit.

I want to expand further on the subject in this answer.

Restarting Powershell also wasn't sufficient to propagate the change. I had to Open Task Manager and restart explorer.exe to trigger a reload of the registry.

It can be quite tedious to navigate the registry so in order to maintain an user friendly experience you can execute this from Powershell:

```
REG ADD "HKCU\Software\Microsoft\Windows\CurrentVersion\Applets\Regedit" /v "LastKey" /d "HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment" /f; regedit
```

It sets the last opened window to a certain registry path, so that when you open regedit the next time it opens at the proper key.

[1]:

[To see links please register here]

Reply

#19
If you need to set variable name dynamically and for session only, then use:

New-Item env:\$key -Value $value -Force | Out-Null
Reply

#20
If, some time during a PowerShell session, you need to see or to _temporarily_ modify the PATH environment variable , you can type one of these commands:

```powershell
$env:Path # shows the actual content
$env:Path = 'C:\foo;' + $env:Path # attach to the beginning
$env:Path += ';C:\foo' # attach to the end

```
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through