PowerShell icon indicating copy to clipboard operation
PowerShell copied to clipboard

PowerShell doesn't delete file in $env:TEMP if account contains a cyrillic char

Open farag2 opened this issue 2 years ago • 16 comments

Prerequisites

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

farag2 avatar Jan 14 '24 19:01 farag2

After more than 30 years Windows still cannot get the 8.3 limitation and Unicode straight...

michael-o avatar Jan 14 '24 21:01 michael-o

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

mklement0 avatar Jan 14 '24 21:01 mklement0

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

Use the same workaround in my module.

farag2 avatar Jan 14 '24 22:01 farag2

Intersting, does this work?

del temp:\1.txt

PS. Use utf-8 as system wide locale, 2024 already.

237dmitry avatar Jan 14 '24 22:01 237dmitry

Intersting, does this work?

del temp:\1.txt

PS. Use utf-8 as system wide locale, 2024 already.

It works in CMD.

farag2 avatar Jan 14 '24 22:01 farag2

It works in CMD.

The filename, directory name, or volume label syntax is incorrect.

I am about pwsh

237dmitry avatar Jan 14 '24 22:01 237dmitry

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.

farag2 avatar Jan 14 '24 22:01 farag2

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\

237dmitry avatar Jan 14 '24 22:01 237dmitry

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 avatar Jan 14 '24 22:01 farag2

@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 TEMP environment variable.
    • By contrast, cmd.exe's internal del command 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:USERPROFILE does not use an 8.3 path

mklement0 avatar Jan 14 '24 23:01 mklement0

It turned out, if a username has the first capital letter Тест (not тест), the bug cannot be reproduced!

farag2 avatar Jan 16 '24 20:01 farag2

изображение

farag2 avatar Jan 23 '24 20:01 farag2

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.

jascott1717 avatar Feb 16 '24 16:02 jascott1717

изображение

farag2 avatar Feb 23 '24 17:02 farag2

изображение

farag2 avatar May 25 '24 09:05 farag2