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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Whitequill Riclo
  • 788
  • 2
  • 8
  • 19
  • I don't think it's a duplicate, since the other question never mentions `Set-Variable` at all. – Eris Jan 01 '16 at 19:49
  • 1
    That's probably because `Set-Variable` is for setting PowerShell variables, *not* for setting environment variables. – Ansgar Wiechers Jan 01 '16 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 01 '16 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 02 '16 at 11:27
  • Related post - [Powershell Add System Variable](https://stackoverflow.com/q/32730544/465053) – RBT Mar 01 '21 at 07:57

1 Answers1

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:\")
Eris
  • 7,378
  • 1
  • 30
  • 45
  • 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. – Whitequill Riclo Jan 02 '16 at 02:12