Create a folder alias in PowerShell

隐身守侯 提交于 2019-12-12 12:18:29

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!