经常使用的目录

时间:2014-01-29 19:41:34

标签: powershell powershell-v3.0

我有大约3或4个目录,我经常在我的机器上使用,并希望能够轻松直接进入这些目录,而不是总是输入它们。

我能想到的最好方法是设置环境变量。但是,执行“cd env:”不起作用。

无论如何对最佳方法有任何想法吗?

* 编辑1 * 我希望有一种方法,一旦我的会话结束,我就不会丢失(例如,关闭PS窗口。)。

5 个答案:

答案 0 :(得分:5)

您可以为个人资料中的每个人创建一个PS驱动器:

New-PSDrive Dir1 -PSProvider FileSystem -Root 'c:\windows\system32'
New-PSDrive Dir2 -PSProvider FileSystem -Root 'c:\program files\Common Files'
New-PSDrive Dir3 -PSProvider FileSystem -Root 'C:\ProgramData\Microsoft\Crypto\RSA\MachineKeys'

然后只需CD或SL到驱动器名称:

cd dir1:
sl dir2:

答案 1 :(得分:4)

您可以在PowerShell profile脚本中创建指向各种文件夹的HashTable。然后,只需使用简写引用它们:

$FL = @{
    Dir1 = 'c:\windows\system32'
    Dir2 = 'c:\program files\Common Files'
    Dir3 = 'C:\ProgramData\Microsoft\Crypto\RSA\MachineKeys'
}

cd $FL.Dir1;
cd $FL.Dir2;
cd $FL.Dir3;

或者,您可以开发小函数,并将它们放入PowerShell配置文件脚本中。

function sys32 {
    [CmdletBinding()]
    param ()
    Set-Location -Path 'c:\windows\system32';
}

function mkeys {
    [CmdletBinding()]
    param ()
    Set-Location -Path 'C:\ProgramData\Microsoft\Crypto\RSA\MachineKeys';
}

function cf {
    [CmdletBinding()]
    param ()
    Set-Location -Path 'C:\Program Files\Common Files';
}

# Call the functions
sys32;
mkeys;
cf;

答案 2 :(得分:4)

profile中为每个功能创建一个小功能。

function gohome {
    set-location c:\users\username
}

答案 3 :(得分:2)

使用powershell中的notepad $ profile获取您的个人资料。

将上述功能之一放入其中并重启powershell。

答案 4 :(得分:1)

有些读者可能比每位用户$profile更喜欢机器范围的个人资料。如果是,请在此位置编辑或创建文件。

%windir%\system32\WindowsPowerShell\v1.0\profile.ps1
IIRC,它在用户配置文件加载之前运行,无论这是你决定的优势/劣势。

相关问题