PowerShell: windows: '~' Home reference bug

Prerequisites

Steps to reproduce

when i update PowerShell in my windows 11 workstation version to 7.4.0 below is version $ $PSVersionTable

Name Value


PSVersion 7.4.0 PSEdition Core GitCommitId 7.4.0 OS Microsoft Windows 10.0.22621 Platform Win32NT PSCompatibleVersions {1.0, 2.0, 3.0, 4.0…} PSRemotingProtocolVersion 2.3 SerializationVersion 1.1.0.1 WSManStackVersion 3.0

when i use the ~ to refer my work home, that may conjunct with current path; at old version that method like resolve ~ to $HOME environment variation. Hope can resolve this problem. thanks

Expected behavior

ps> more ~\test
1

Actual behavior

# xxxxxx @ SHC5CG33131WZL in ~\AppData\Local\Temp\tmphcnstt.tmp [18:41:39]
$ more ~\test
Cannot access file C:\Users\xxxxxx\AppData\Local\Temp\tmphcnstt.tmp\~\test

Error details

No response

Environment data

Name                           Value
----                           -----
PSVersion                      7.4.0
PSEdition                      Core
GitCommitId                    7.4.0
OS                             Microsoft Windows 10.0.22621
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Visuals

No response

About this issue

  • Original URL
  • State: open
  • Created 7 months ago
  • Comments: 18 (3 by maintainers)

Most upvoted comments

To summarize:

  • On Windows, ~ is only recognized by the provider cmdlets, such as Get-Content and Get-ChildItem (there it refers to the home location of the provider underlying the current location; for the FileSystem provider, that is $HOME, i.e. the current user’s home directory (same as $env:USERPROFILE on Windows).

  • External programs - such as more.com or code - do not understand ~ and - unlike on Unix-like platforms - on Windows PowerShell performs no translation of ~ to the value of $HOME behind the scenes.

  • In Windows Powershell more ~\test happened to work, because there more refers to a PowerShell function by that name (run $function:more to see its body) that wraps more.com and passes the file-path argument to Get-Content, which, as stated, does understand ~.

  • Because this function was removed in PowerShell Core, more ~\test now passes ~\test directly to more.com, which therefore fails (because more.com doesn’t understand ~).

    • Up to v7.3 you could use tab-completion to work around that problem, as tab-completing a ~-prefixed path would instantly convert ~ to the value of $HOME - this was changed in 7.4.0; again, see #20750

So have u fix it or add this feature?

I suggested

Work around, if you mean $HOME in PowerShell then use $HOME.

more $HOME\test

Tilde expansion is not consistent, UNIX shell always expands it unless it is escaped. PowerShell does not, for instance

PS> Write-Host ~\test
~\test

But if you replace the tilde with $HOME it does expand it

PS> Write-Host $HOME\test
C:\Users\bythesea\test

Nice summary of several recent topics.

Curious. It works for type and Get-Content, but not more.

Is it really using more.com? ( blast from the past of PC-DOS 2.0 )

c:\windows\system32\more.com

It looks like you found the issue when using an external command.

I suggest this is not a bug because C:\WINDOWS\system32\cmd.exe does not expand tilde so existing native commands are not designed to handle that expansion.

Work around, if you mean $HOME in PowerShell then use $HOME.