8

I am having trouble replicating this command from CMD set APPDATA=D:\.

The best equivalent I have is Set-Variable -Name $env:APPDATA -Value D:\. This does not work!

The full script is:

set APPDATA=D:\
start java -jar D:\.minecraft\minecraft.jar

This sets it so Java looks in D:\ for .minecraft instead of APPDATA.

The full PowerShell version (which doesn't work right) is:

& Set-Variable -Name $env:APPDATA -Value D:\
& 'C:\Program Files\Java\jre1.8.0_66\bin\java.exe' -jar D:\.minecraft\Minecraft.jar

It still looks at the read-only version of $env:APPDATA. I don't see why it can't be changed in the running environment for the shell's session, like cmd and most *nix shells!

I'm sure there are more uses for this than just running Minecraft. :P

5
  • I don't think it's a duplicate, since the other question never mentions Set-Variable at all.
    – Eris
    Jan 1, 2016 at 19:49
  • 1
    That's probably because Set-Variable is for setting PowerShell variables, not for setting environment variables. Jan 1, 2016 at 21:27
  • The other question doesn't actually say that Set-Variable is not for environment variables in any of the answers, nor was it mentioned in the question. While the solution is similar, it is not a duplicate question.
    – Eris
    Jan 1, 2016 at 22:48
  • While you're right about Set-Variable, it's sad that the question how to set an env variable with PowerShell has already been asked several times, and we don't have a canonical Q to link to
    – sodawillow
    Jan 2, 2016 at 11:27
  • Related post - Powershell Add System Variable
    – RBT
    Mar 1, 2021 at 7:57

1 Answer 1

10

When you use Set-Variable the -Name argument should NOT have the dollar sign ($).

# WRONG: Set's the variable using the name stored in ENV:APPDATA
Set-Variable -Name $env:APPDATA -Value D:\

Also, Set-Variable doesn't appear to work as expected with environment variables. I'm assuming this is because Set-Variable is specifically designed to work with Variable: variable provider, rather than the env: environment provider. (I verified this locally, using Set-Variable -Name ENV:AppData -Value "foo" created a variable called 'ENV:AppData' in the Variable: provider.)

Alternative 1 (simple format):

$ENV:AppData="D:\"
& 'C:\Program Files\Java\jre1.8.0_66\bin\java.exe' -jar D:\.minecraft\Minecraft.jar

Alternative 2 (using C# API):

[Environment]::SetEnvironmentVariable("APPDATA", "D:\")
1
  • Thank you very much! I did look around some, but I couldn't find anything. I am fairly new to the Powershell and didn't know about the Variable: provider vs the Environment provider. I was starting to guess that there was a local variable vs a global environment variable, but I couldn't find anything saying that on official documentation by Microsoft, or I wasn't looking in the right place. Jan 2, 2016 at 2:12

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.