8

Please help me to deal with PowerShell environment variable.

I had learned that PowerShell has special syntax to access the system environment variable values. So I've tried to execute:

$env:Temp

in the PowerShell 6.2.4 console on Windows 10. The output is strange:

C:\Users\OD42B~1.BOR\AppData\Local\Temp

It has my Windows user name shortened to the 8.3 form. The problem is that I can't use the cd $env:temp command, it displays the following error:

cd : An object at the specified path C:\Users\OD42B~1.BOR does not exist.

But I can do cd %temp% in the cmd terminal.

I've tried Windows PowerShell app (%SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe) but cd $env:Temp does not works either.

I've checked environment variable configuration option (Sytem properties\Environment variables) and both TEMP and TMP variables are shown using the long form profile folder name, like c:\users\o.borolongprofilename\AppData\Local\Temp.

How can I make cd $env:Temp work for a non-8.3 profile name in the PowerShell?

UPD

  • PowerShell get-childitem env:Temp output is C:\Users\OD42B~1.BOR\AppData\Local\Temp.

  • Cmd echo %temp% output is the same C:\Users\OD42B~1.BOR\AppData\Local\Temp (but cd %temp% works in the cmd).

  • TEMP environment variable in the UI is set properly (value shown is like c:\users\o.borolongprofilename\AppData\Local\Temp).

    However TEMP value is shown like %USERPROFILE%\AppData\Local\Temp when I try to edit TEMP using the UI.

  • PowerShell get-childitem env:userprofile displays full name like c:\users\o.borolongprofilename (surprise).

UPD2

I've just checked PowerShell 7 rc2 but the result is the same: cd $env:Temp does not work.

UPD3

Thank you for helping me. I've found the answer provided by the @Smock comment link:

cd (gi $env:temp).fullname

14
  • What value does get-childitem env:Temp return ? Also check the environment variables set via the gui interface - is it 8.3 there? What does echo %Temp% from cmd prompt give you?
    – Smock
    Feb 12, 2020 at 11:50
  • @Smock thanks for helping me. Please find question updated with answers you have requested.
    – oleksa
    Feb 12, 2020 at 12:55
  • 1
    Perhaps this will provide a solution (if not a fix)? cd (gi $env:temp).fullname
    – Smock
    Feb 12, 2020 at 13:52
  • cd "$env:temp" Feb 15, 2020 at 4:49
  • 1
    When you say that it didn't work, you have to say how it didn't work. Feb 17, 2020 at 14:20

3 Answers 3

1

Thank you for helping me. I've found the answer provided by the @Smock comment link:

cd (gi $env:temp).fullname

1

I don't know what causes this, but you could add a small piece of powershell in your profile:

$env:TEMP = [Environment]::GetEnvironmentVariable("Temp", [EnvironmentVariableTarget]::User)

That way, $env:Temp will be updated each time you launch PowerShell

0

You can also get the path from registry value and use it:

But not using this one: HKCU\Environment

[HKCU:\Environment]
@TEMP=%USERPROFILE%\AppData\Local\Temp
TEMP    REG_EXPAND_SZ    %USERPROFILE%\AppData\Local\Temp

The output also came with: %USERPROFILE%, so....



...try using this registry key: HKCU\Environment

[HKCU:\Volatile Environment]
@LOCALAPPDATA=C:\Users\ecker\AppData\Local
LOCALAPPDATA    REG_SZ    C:\Users\ecker\AppData\Local

And concatenate only the output sequence with:TEMP ==> "$path_str\TEMP"


$path_str = (Get-ItemProperty "HKCU:\Volatile Environment")|Select-Object -ExpandProperty LOCALAPPDATA | Resolve-path ; cd "$path_str\TEMP"

# Or using aliases #

$path_str = (gp "HKCU:\Volatile Environment")|select -ExpandProperty LOCALAPPDATA | rvpa ; cd "$path_str\TEMP"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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