1

On my computer, the $env:temp variable contains a tilde:

PS C:\> echo $env:temp
C:\Users\MOON~1.REN\AppData\Local\Temp

When I try to cd into the directory that $env:temp points to,the following error:

PS C:\> cd $env:temp
cd : An object at the specified path C:\Users\MOON~1.REN does not exist.

Is there a possibility to force PowerShell to expand the short name of the temp directory to its full name?

Is something fishy going on?

It was pointed out that maybe something fishy is going on. This seems indeed to be the case. In cmd.exe:

C:\>echo %username%
Rene.Nyffenegger

C:\>echo %userprofile%
C:\Users\MOON.Rene

C:\>echo %temp%
C:\Users\MOON~1.REN\AppData\Local\Temp

C:\>cd %temp%

C:\Users\MOON~1.REN\AppData\Local\Temp>echo %date% > iWasHere

C:\Users\MOON~1.REN\AppData\Local\Temp>cd Users\MOON.Rene\AppData\Local\Temp

C:\Users\MOON.Rene\AppData\Local\Temp>type iWasHere
Fri 10/25/2019

The interesting thing is: if in cmd.exe, I can cd to C:\Users\MOON~1.Ren but I can't do that in PowerShell:

C:\> cd C:\Users\MOON~1.Ren
C:\Users\MOON~1.Ren

PS C:\> cd C:\Users\MOON~1.Ren
cd : An object at the specified path C:\Users\MOON~1.Ren does not exist.

So, I really don't know what's going on here.

2
  • 1
    Are you sure this path actually exists? Normally ~1 paths have 6 characters in front of it, not 4. If you open a command prompt and type in cd %TEMP% will that work? if neither, then your TEMP variable is incorrect.
    – LPChip
    Oct 25, 2019 at 14:48
  • 1
    Yes, in cmd.exe, cd %temp% takes me to C:\Users\MOON~1.REN\AppData\Local\Temp Oct 25, 2019 at 17:02

2 Answers 2

0

To answer strictly the question that appears in the title of this web-page: one just does.

The Tilde Character within the MicroSoft Windows PowerShell Integrated Shell-Scripting Environment

>_ Windows PowerShell
Windows PowerShell
Copyright (C) MicroSoft Corporation. All rights reserved.

Try the new cross-platform PowerShell https://aka.ms/pscore6

PS C:\Users\DOBRE> CD "C:\Users\DOBRE\OneDrive\Documents\MOON~1.REN\"
PS C:\Users\DOBRE\OneDrive\Documents\MOON~1.REN> CD \
PS C:\> CD C:\Users\DOBRE\OneDrive\Documents\MOON~1.REN\
PS C:\Users\DOBRE\OneDrive\Documents\MOON~1.REN>

However, as @LPChip has noticed with an extremely good attention (who would have thought of having two characters less before the tilde character?), something is fishy with that User Name, namely "MOON~1.REN". Is that "REN" also coming from "René"?

To sum up: there is nothing wrong with the Tilde Character within the MicroSoft Windows PowerShell Integrated Shell-Scripting Environment. In the example that appears above, it is treated as any other character. The example has been manually created and it is not representing a shortened version of a longer name.

Still, there are two issues left:

  1. How has that "MOON~1.REN" User Name come into being?

  2. Examining strictly this Error Message: "cd: An object at the specified path C:\Users\MOON~1.REN does not exist", the "C:\Users\MOON~1.REN" path does not appear to be associable with an object.

0
0

TL;DR: Get the long version of the 8.3 filename and cd to it:

cd (gi $env:temp).FullName

Everything is ok with your environment variable. It just contains your user profile folder name in the 8.3 filename format. Many years ago, this notation was necessary to make longer filenames accessible for systems that did not already support longer filenames. Btw., in your case, your folder name is not treated as a folder name, but as a filename, because it contains a dot.

You can handle such names with a workaround for cd in PowerShell. First, let's reproduce your problem. To view the 8.3 filenames in PowerShell, you can use the following function (taken from here):

function Get-ShortName {

    Begin {
        $fso = New-Object -ComObject Scripting.FileSystemObject
    }

    Process {
        If ($_.psiscontainer) {
            $fso.getfolder($_.fullname).ShortName
        } else {
            $fso.getfile($_.fullname).ShortName
        }
    }
}

Let's create your folder on C: and display its 8.3 name:

cd C:\
New-Item -Name 'MOON.Rene' -ItemType Directory | Get-ShortName

Output:

MOON~1.REN

Changing your directory to it indeed throws an exception:

PS C:\> cd MOON~1.REN
cd : An object at the specified path C:\MOON~1.REN does not exist.
At line:1 char:1
+ cd MOON~1.REN
+ ~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Set-Location], PSArgumentException
    + FullyQualifiedErrorId : Argument,Microsoft.PowerShell.Commands.SetLocationCommand

As a workaround, you can evaluate the the 8.3 filename to its full name and change your directory to this instead:

cd (gi MOON~1.REN).FullName

Which will work:

PS C:\MOON.Rene>

FYI, this is the short version of the following line:

Set-Location -Path (Get-Item -Path MOON~1.REN).FullName

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .