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:
- 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
- I then checked to see if the
cmd
set
command beared the same results asgci env:
and it did, so I then tried to usesetx
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"
[System.Environment]::GetEnvironmentVariable()
static method. it accepts machine/user/process for the exact environment, and the variable name. the matchingSet
does the same and accepts a value./m
to your setx command:setx testbat "%HOMEDRIVE%%HOMEPATH%\Desktop\Programs\test.bat" /m