PowerShell doesn't delete file in $env:TEMP if account contains a cyrillic char
Prerequisites
- [X] Write a descriptive title.
- [X] Make sure you are able to repro it on the latest released version
- [X] Search the existing issues.
- [X] Refer to the FAQ.
- [X] Refer to Differences between Windows PowerShell 5.1 and PowerShell.
Steps to reproduce
If a user account contains a Cyrillic char in its name, PowerShell is unable to remove any file in $env:TEMP folder with command Remove-Item "$env:TEMP\1.txt" -Force.
The issue cannot be reproduced with $env:LOCALAPPDATA and $env:APPDATA.
Added on 16.01.2024
It turned out, if a username has the first capital letter Тест (not тест), the bug cannot be reproduced!
Expected behavior
File should be removed.
Actual behavior
Remove-Item: An object at the specified path C:\Users\FF6D~1 does not exist.
Error details
PS C:\Users\тест> get-error
Exception :
Type : System.Management.Automation.PSArgumentException
ErrorRecord :
Exception :
Type :
System.Management.Automation.ParentContainsErrorRecordException
Message : An object at the specified path C:\Users\FF6D~1 does
not exist.
HResult : -2146233087
CategoryInfo : InvalidArgument: (:) [],
ParentContainsErrorRecordException
FullyQualifiedErrorId : Argument
Message : An object at the specified path C:\Users\FF6D~1 does not
exist.
ParamName : path
TargetSite :
Name : NormalizeThePath
DeclaringType : Microsoft.PowerShell.Commands.FileSystemProvider
MemberType : Method
Module : System.Management.Automation.dll
Source : System.Management.Automation
HResult : -2147024809
StackTrace :
at
Microsoft.PowerShell.Commands.FileSystemProvider.NormalizeThePath(String
basepath, Stack`1 tokenizedPathStack)
at Microsoft.PowerShell.Commands.FileSystemProvider.NormalizeRelativePath
Helper(String path, String basePath)
CategoryInfo : InvalidArgument: (:) [Remove-Item],
PSArgumentException
FullyQualifiedErrorId :
Argument,Microsoft.PowerShell.Commands.RemoveItemCommand
InvocationInfo :
MyCommand : Remove-Item
ScriptLineNumber : 1
OffsetInLine : 1
HistoryId : 1
Line : Remove-Item "$env:TEMP\1.txt" -Force
Statement : Remove-Item "$env:TEMP\1.txt" -Force
PositionMessage : At line:1 char:1
+ Remove-Item "$env:TEMP\1.txt" -Force
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
InvocationName : Remove-Item
CommandOrigin : Internal
ScriptStackTrace : at <ScriptBlock>, <No file>: line 1
Environment data
Name Value
---- -----
PSVersion 7.4.1
PSEdition Core
GitCommitId 7.4.1
OS Microsoft Windows 10.0.22631
Platform Win32NT
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
WSManStackVersion 3.0
Visuals
Workaround
Get-ChildItem -Path "$env:TEMP\1.txt" | Remove-Item
After more than 30 years Windows still cannot get the 8.3 limitation and Unicode straight...
The bug also affects Windows PowerShell, and switching from the (positionally implied) -Path parameter to -LiteralPath doesn't make a difference.
It's not a fundamental problem, however, given the following workaround:
Get-Item $env:TEMP\1.txt | Remove-Item
The bug also affects Windows PowerShell, and switching from the (positionally implied)
-Pathparameter to-LiteralPathdoesn't make a difference.It's not a fundamental problem, however, given the following workaround:
Get-Item $env:TEMP\1.txt | Remove-Item
Use the same workaround in my module.
Intersting, does this work?
del temp:\1.txt
PS. Use utf-8 as system wide locale, 2024 already.
Intersting, does this work?
del temp:\1.txtPS. Use utf-8 as system wide locale, 2024 already.
It works in CMD.
It works in CMD.
The filename, directory name, or volume label syntax is incorrect.
I am about pwsh
It works in CMD.
The filename, directory name, or volume label syntax is incorrect.I am about pwsh
PS C:\Users\тест> del temp\1.txt
Remove-Item: Cannot find path 'C:\Users\тест\temp\1.txt' because it does not exist.
тест means test in Russian. It's a VM in Hyper-V.
Ok, I thought it might be possible using PSDrive temp: this would work
not temp\1.txt but temp:\1.txt
PS > Get-PSDrive -Name Temp
Name Used (GB) Free (GB) Provider Root
---- --------- --------- -------- ----
Temp 40,20 197,30 FileSystem C:\Users\<user>\AppData\Local\Temp\
Ok, I thought it might be possible using PSDrive
temp:this would work
I will until the bug will be fixed properly. I'm OK to use now Get-Item $env:TEMP\1.txt | Remove-Item in the code.
@farag2, a tip for future issues:
- If you already know of a workaround, please mention it as part of the initial post.
- An "I already knew that" comment in response to somebody else mentioning a workaround doesn't meaningfully contribute to the conversation.
Given the comments above, the bug seems to come down to this:
- Use of
Remove-Item... - ... in combination with the 8.3 form of a path, as reflected in the
TEMPenvironment variable.- By contrast,
cmd.exe's internaldelcommand does not have this problem - Similarly, using the PowerShell-only
Temp:drive works fine - Ditto for
Remove-Item $env:USERPROFILE\AppData\Local\Temp\1.txt, given that$env:USERPROFILEdoes not use an 8.3 path
- By contrast,
It turned out, if a username has the first capital letter Тест (not тест), the bug cannot be reproduced!
I encountered a similar issue with a coworker. His name is JT, two consecutive capital letters, and he was encountering the exact same issue trying to delete a file in the temp directory. We applied the workaround described above and were able to get past it.