问题
I know that I can create variable that represents a folder path in my profile. For example,
$here = Split-Path -Parent $MyInvocation.MyCommand.Path
Is there an easy way to create an alias to a directory in PowerShell?
Create an alias
PS> Create-FolderAlias -name $foo -path "C:\Program Files"
Create an alias based on another alias
PS> Create-FolderAlias -name $bar -path $foo + "\Microsoft"
Use alias as expected
PS> cd $foo
It would be nice if these aliases would be persisted between sessions.
回答1:
You can turn a folder into a new powershell drive with New-PSDrive
New-PSDrive foo filesystem 'C:\Program Files'
New-PSDrive bar filesystem 'foo:\Microsoft'
cd foo:
To persist between sessions you could add them to your profile script ($profile).
But of course you can also cd to a folder from a variable
$foo = 'C:\Program Files'
$bar = Join-Path $foo 'Microsoft'
cd $foo
回答2:
An alternative solution is to write a function that cd's to that directory. For me, this yields the least amount of typing to get where I want. For instance, I put the following function in my profile.ps1:
# quickly cd to folder
function <short name> {
cd <path to directory>
}
Then, in Powershell:
PS> <short name>
来源:https://stackoverflow.com/questions/16597839/create-a-folder-alias-in-powershell