PowerShell: windows: '~' Home reference bug
Prerequisites
- Write a descriptive title.
- Search the existing issues.
- Make sure you are able to repro it on the latest released version
- Refer to the FAQ.
- Refer to Differences between Windows PowerShell 5.1 and PowerShell.
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)
To summarize:
On Windows,
~is only recognized by the provider cmdlets, such asGet-ContentandGet-ChildItem(there it refers to the home location of the provider underlying the current location; for theFileSystemprovider, that is$HOME, i.e. the current user’s home directory (same as$env:USERPROFILEon Windows).External programs - such as
more.comorcode- do not understand~and - unlike on Unix-like platforms - on Windows PowerShell performs no translation of~to the value of$HOMEbehind the scenes.In Windows Powershell
more ~\testhappened to work, because theremorerefers to a PowerShell function by that name (run$function:moreto see its body) that wrapsmore.comand passes the file-path argument toGet-Content, which, as stated, does understand~.Because this function was removed in PowerShell Core,
more ~\testnow passes~\testdirectly tomore.com, which therefore fails (becausemore.comdoesn’t understand~).~-prefixed path would instantly convert~to the value of$HOME- this was changed in 7.4.0; again, see #20750I suggested
Tilde expansion is not consistent, UNIX shell always expands it unless it is escaped. PowerShell does not, for instance
But if you replace the tilde with $HOME it does expand it
Nice summary of several recent topics.
Curious. It works for
typeandGet-Content, but notmore.Is it really using more.com? ( blast from the past of PC-DOS 2.0 )
It looks like you found the issue when using an external command.
I suggest this is not a bug because
C:\WINDOWS\system32\cmd.exedoes not expand tilde so existing native commands are not designed to handle that expansion.Work around, if you mean
$HOMEin PowerShell then use$HOME.