The problem
I am trying to change the expanded path of the ~ character in PowerShell without changing the $Env:USERPROFILE variable.
What I've tried
My initial approach was to create an alias to a function that references a differen environment variable, but it doesn't seem to work.
My code:
function Get-HomeDirectory {
# This is a custom function, it works as I have tested it
# It should be self-explanatory
Get-EnvironmentVariable -Key $HOME_DIR_KEY -User
}
Set-Alias -Name ~ -Value Get-HomeDirectory
Result
If I use Get-Help it works as expected:
PS> Get-Help ~
NAME
Get-HomeDirectory
SYNOPSIS
Returns the home directory of the current user.
SYNTAX
Get-HomeDirectory [<CommonParameters>]
DESCRIPTION
Retrieves the value set for the `$Env:USER_HOME_DIR` environment variable.
RELATED LINKS
Set-HomeDirectory
REMARKS
To see the examples, type: "Get-Help Get-HomeDirectory -Examples"
For more information, type: "Get-Help Get-HomeDirectory -Detailed"
For technical information, type: "Get-Help Get-HomeDirectory -Full"
For online help, type: "Get-Help Get-HomeDirectory -Online"
But if I try to use it I get:
PS> cd ~
PS> pwd
C:\Users\myuser
What works properly
Nevertheless, I can make it work if I pass it with a pipe (as it should), but that's not a very convenient way of using it:
PS> ~ | cd
PS> pwd
Path
----
B:\