0

In cmd, you could access cmd environment variables outside of cmd, but is there a way to accomplish that is powershell?


BACKGROUND:
In command-prompt (cmd.exe), there are environment variables like %COMSPEC% and %PROGRAMFILES% that you could use outside of cmd an din file explorer and other places (i. e. shortcuts) just by typing in %VariableName% somewhere (this is how I see a lot of people accessing their appdata folders) but I have yet to find a way to do it in powershell. In cmd, you could even define a variable with setx nd be able to use it outside of cmd. Ex:

setx testbat "%HOMEDRIVE%%HOMEPATH%\Desktop\Programs\test.bat"

Then you could use it like this in shortcuts:

%testbat%

Shortcut Wizard with cmd variable

However, I have not been able to replicate this with Powershell variables.


What I've tried:

  1. I saw this and wondered if making an environment variable in powershell with this in mind could be possible:
$env:test = "C:\Users\Neko"

Then I restarted and tried using this both in cmd and in the search prompt (search prompt)

C:\Users\Neko>cd %test%
The system cannot find the path specified.
C:\Users\Neko>echo %test%
%test%

Sure enough, it didn't even show up in powershell when I did gci env: after the restart Since this didn't work I deleted the variable and had an idea:

PS C:\Users\Neko> gci env:

Name                           Value
----                           -----
ALLUSERSPROFILE                C:\ProgramData
APPDATA                        C:\Users\Neko\AppData\Roaming
CommonProgramFiles             C:\Program Files\Common Files
CommonProgramFiles(x86)        C:\Program Files (x86)\Common Files
CommonProgramW6432             C:\Program Files\Common Files
COMPUTERNAME                   XXXXXXXXX
ComSpec                        C:\Windows\system32\cmd.exe
DriverData                     C:\Windows\System32\Drivers\DriverData
HOMEDRIVE                      C:
HOMEPATH                       \Users\Neko
LOCALAPPDATA                   C:\Users\Neko\AppData\Local
LOGONSERVER                    \\XXXXXXXXX
NUMBER_OF_PROCESSORS           8
OneDrive                       C:\Users\Neko\OneDrive
OneDriveConsumer               C:\Users\Neko\OneDrive
OS                             Windows_NT
POWERSHELL_DISTRIBUTION_CHA... MSI:Windows 10 Pro Education
PROCESSOR_ARCHITECTURE         AMD64
PROCESSOR_IDENTIFIER           Intel64 Family 6 Model 126 Stepping 5, GenuineIntel
PROCESSOR_LEVEL                6
PROCESSOR_REVISION             7e05
ProgramData                    C:\ProgramData
ProgramFiles                   C:\Program Files
ProgramFiles(x86)              C:\Program Files (x86)
ProgramW6432                   C:\Program Files
PSModulePath                   C:\Users\Neko\Documents\WindowsPowerShell\Modules;C:\Program Files\WindowsPowerShell\M...
PUBLIC                         C:\Users\Public
SystemDrive                    C:
SystemRoot                     C:\Windows
TEMP                           C:\Users\Neko\AppData\Local\Temp
test                           C:\Users\Neko
TMP                            C:\Users\Neko\AppData\Local\Temp
USERDOMAIN                     XXXXXXXXX
USERDOMAIN_ROAMINGPROFILE      XXXXXXXXX
USERNAME                       Neko
USERPROFILE                    C:\Users\Neko
windir                         C:\Windows
WSLENV                         WT_SESSION::WT_PROFILE_ID
WT_PROFILE_ID                  XXXXXXXXX
WT_SESSION                     XXXXXXXXX


PS C:\Users\Neko> del "env:test"
PS C:\Users\Neko> gci env:

Name                           Value
----                           -----
ALLUSERSPROFILE                C:\ProgramData
APPDATA                        C:\Users\Neko\AppData\Roaming
CommonProgramFiles             C:\Program Files\Common Files
CommonProgramFiles(x86)        C:\Program Files (x86)\Common Files
CommonProgramW6432             C:\Program Files\Common Files
COMPUTERNAME                   XXXXXXXXX
ComSpec                        C:\Windows\system32\cmd.exe
DriverData                     C:\Windows\System32\Drivers\DriverData
HOMEDRIVE                      C:
HOMEPATH                       \Users\Neko
LOCALAPPDATA                   C:\Users\Neko\AppData\Local
LOGONSERVER                    \\XXXXXXXXX
NUMBER_OF_PROCESSORS           8
OneDrive                       C:\Users\Neko\OneDrive
OneDriveConsumer               C:\Users\Neko\OneDrive
OS                             Windows_NT
Path                           C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPo...
PATHEXT                        .COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC;.CPL
POWERSHELL_DISTRIBUTION_CHA... MSI:Windows 10 Pro Education
PROCESSOR_ARCHITECTURE         AMD64
PROCESSOR_IDENTIFIER           Intel64 Family 6 Model 126 Stepping 5, GenuineIntel
PROCESSOR_LEVEL                6
PROCESSOR_REVISION             7e05
ProgramData                    C:\ProgramData
ProgramFiles                   C:\Program Files
ProgramFiles(x86)              C:\Program Files (x86)
ProgramW6432                   C:\Program Files
PSModulePath                   C:\Users\Neko\Documents\WindowsPowerShell\Modules;C:\Program Files\WindowsPowerShell\M...
PUBLIC                         C:\Users\Public
SystemDrive                    C:
SystemRoot                     C:\Windows
TEMP                           C:\Users\Neko\AppData\Local\Temp
TMP                            C:\Users\Neko\AppData\Local\Temp
USERDOMAIN                     XXXXXXXXX
USERDOMAIN_ROAMINGPROFILE      XXXXXXXXX
USERNAME                       Neko
USERPROFILE                    C:\Users\Neko
windir                         C:\Windows
WSLENV                         WT_SESSION::WT_PROFILE_ID
WT_PROFILE_ID                  XXXXXXXXX
WT_SESSION                     XXXXXXXXX
  1. I then checked to see if the cmd set command beared the same results as gci env: and it did, so I then tried to use setx to see if it appeared in powershell as well:
setx test "Test"

And after a restart, it did

PS C:\Users\Neko> $env:test
Test

I realized that the variables all were stored in the registry and that I could edit the registry of course with:

Set-Itemproperty -path 'HKCU:\Environment' -Name 'Test' -value 'Test'

But it doesn't feel the same as something like setx


CONCLUSION:
I want to learn if there is a true-powershell-esque command that can define environemnt variables that I can use outside of powershell like setx can in cmd. I am not looking for full scripts just commands or something like $env:variable = "value".

UPDATE:
Yes, you can create a function to do this as well, technically a one-liner possibly, not what I'm looking for. I want to be able to create environment variables in powershell with built in cmdlets. Something "true-powershell"

Nico Nekoru
  • 2,840
  • 2
  • 17
  • 38
  • take a look at the `[System.Environment]::GetEnvironmentVariable()` static method. it accepts machine/user/process for the exact environment, and the variable name. the matching `Set` does the same and accepts a value. – Lee_Dailey Jun 09 '20 at 20:04
  • Add `/m` to your setx command: `setx testbat "%HOMEDRIVE%%HOMEPATH%\Desktop\Programs\test.bat" /m` – Random Dec 06 '22 at 06:10

1 Answers1

2

Powershell itself provides these methods to interact with environment variables:

  • The Environment provider drive
  • The Item cmdlets
  • The .NET System.Environment class
  • On Windows, the System Control Panel

So, to set a persistent environment variable (PSModulePath) to a value $newpath at machine level you might use

[Environment]::SetEnvironmentVariable("PSModulePath", $newpath, 'Machine')

To get an environment variable you might use

$path = [Environment]::GetEnvironmentVariable('PSModulePath', 'Machine')

or the variable syntax

$Env:PSModulePath

Here's the documentation.

Peter Schneider
  • 2,879
  • 1
  • 14
  • 17
  • When I do this, 1. It doesn't show up when I do the "variable syntax" `$env:name` and 2 does not allow me to use it if non ps context. Doesn't affect the registry either so I think this is only powershell variable. Also does not show up in `gci env:` – Nico Nekoru Jun 09 '20 at 20:07
  • 2
    Environment variables are read at startup... so you have to restart powershell and then the $env:name will show up... There's also another [SO question](https://stackoverflow.com/questions/14381650/how-to-update-windows-powershell-session-environment-variables-from-registry) about this.... – Peter Schneider Jun 09 '20 at 20:10
  • thank you, I restarted and now it works. Can you explain how this works because as far as I can see it doesn't change anything in the registry or in other places? – Nico Nekoru Jun 09 '20 at 20:14
  • It's saved to the registry on windows. It depends on the OS your are running and which location you have chosen (Machine, User, Process). See [here](https://learn.microsoft.com/en-us/dotnet/api/system.environment.setenvironmentvariable?view=netcore-3.1). At least for machine level entries the registry location is HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Control\Session Manager\Environment – Peter Schneider Jun 09 '20 at 20:21
  • I see... Is there a specific reason for the first argument to be double-quoted and the last to be single-quoted in your answer? – Nico Nekoru Jun 09 '20 at 20:26
  • No.. both quote methods are valid. The only difference is, that within single quotes no variables are expanded... – Peter Schneider Jun 09 '20 at 20:27