6
0
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 isC:\Users\OD42B~1.BOR\AppData\Local\Temp
.Cmd
echo %temp%
output is the sameC:\Users\OD42B~1.BOR\AppData\Local\Temp
(butcd %temp%
works in the cmd).TEMP
environment variable in the UI is set properly (value shown is likec:\users\o.borolongprofilename\AppData\Local\Temp
).However
TEMP
value is shown like%USERPROFILE%\AppData\Local\Temp
when I try to editTEMP
using the UI.PowerShell
get-childitem env:userprofile
displays full name likec:\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
What value does
get-childitem env:Temp
return ? Also check the environment variables set via the gui interface - is it 8.3 there? What doesecho %Temp%
from cmd prompt give you? – Smock – 2020-02-12T11:50:03.007@Smock thanks for helping me. Please find question updated with answers you have requested. – oleksa – 2020-02-12T12:55:14.283
1
Perhaps this will provide a solution (if not a fix)?
– Smock – 2020-02-12T13:52:24.940cd (gi $env:temp).fullname
cd "$env:temp"
– Bill_Stewart – 2020-02-15T04:49:20.523@Bill_Stewart it does not work in PowerShell pscore6. The same as in 7-rc2 preview – oleksa – 2020-02-17T08:46:46.613
1When you say that it didn't work, you have to say how it didn't work. – Bill_Stewart – 2020-02-17T14:20:54.317
What is the returned value for
[Environment]::GetEnvironmentVariable("Temp")
? – Theo – 2020-02-17T14:41:44.150@Bill_Stewart it fails exactly the same as before
cd : An object at the specified path C:\Users\OD42B~1.BOR does not exist.
, nothing new. Does it work on your side (with non 8.3 profile name)? Actually I've found the solution already (using the @Smock comment) – oleksa – 2020-02-17T14:43:23.350@Theo
[Environment]::GetEnvironmentVariable("Temp")
powershell output isC:\Users\OD42B~1.BOR\AppData\Local\Temp
– oleksa – 2020-02-17T14:45:59.093Ok, then try to find if your Temp variable is not defined multiple times. There are three 'targets' for the environment vars:
Machine
,User
andProcess
. Try ou these commands:[Environment]::GetEnvironmentVariable("Temp", [EnvironmentVariableTarget]::Machine)
,[Environment]::GetEnvironmentVariable("Temp", [EnvironmentVariableTarget]::User)
,[Environment]::GetEnvironmentVariable("Temp", [EnvironmentVariableTarget]::Process)
to see the different values returned – Theo – 2020-02-17T14:50:22.833@Theo
[Environment]::GetEnvironmentVariable("Temp", [EnvironmentVariableTarget]::User)
produces the correct output likeC:\Users\o.borolongprofilename\AppData\Local\Temp
. – oleksa – 2020-02-17T15:02:54.220..and the other commands? (Process in particular)? – Theo – 2020-02-17T15:05:40.143
@Theo
Machine
printsc:\windows\temp
andProcess
outputsC:\Users\OD42B~1.BOR\AppData\Local\Temp
. I've found the answer from @Smock comment thank you. But it is not possible to mark the question as duplicate since the answer is on stackoverflow site. – oleksa – 2020-02-17T15:10:36.123Seems that it has a problem with 8.3 file names (hence why the expanded filename works). When you are in
cmd
, you can usedir /x
to display the 8.3 filenames for files. Determine a short filename and see if PS works with it (gc
, etc.). – Bill_Stewart – 2020-02-17T23:10:30.173