Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Powershell 常用目录_Powershell_Powershell 3.0 - Fatal编程技术网

Powershell 常用目录

Powershell 常用目录,powershell,powershell-3.0,Powershell,Powershell 3.0,我有大约3或4个目录,我经常去我的机器上,并希望有一种方法可以很容易地直接进入这些目录,而不是总是键入它们 我能想到的最好的方法就是设置环境变量。然而,做“cd环境:”是行不通的 不管怎么说,你有没有什么好办法 *编辑1* 我希望有一种方法在会话关闭后不会丢失(例如,关闭PS窗口)。您可以在脚本中创建一个指向各种文件夹的哈希表。然后,简单地用速记法引用它们: $FL = @{ Dir1 = 'c:\windows\system32' Dir2 = 'c:\program file

我有大约3或4个目录,我经常去我的机器上,并希望有一种方法可以很容易地直接进入这些目录,而不是总是键入它们

我能想到的最好的方法就是设置环境变量。然而,做“cd环境:”是行不通的

不管怎么说,你有没有什么好办法

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

您可以在脚本中创建一个指向各种文件夹的
哈希表。然后,简单地用速记法引用它们:

$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;

在for each中创建一个小函数

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

您可以为配置文件中的每个驱动器创建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:

从powershell中使用记事本$profile获取您的个人资料


将上述功能之一放入其中,然后重新启动powershell。

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

%windir%\system32\WindowsPowerShell\v1.0\profile.ps1

IIRC,它在加载用户配置文件之前运行,这是否是您决定的优势/劣势。

这种方法的问题是,一旦关闭PS,就失去了HT。我希望我能做一次,而不必担心每次会话。@Michael:如果你把它放在你的PowerShell配置文件脚本中,你就不必担心每次都要重新定义它。这就是我链接到PowerShell配置文件文档的原因。我错过了。我检查了一下,并尝试了一下。它在使用个人资料后工作得非常好!我用了HT方法。@Michael:很高兴听到这个消息,伙计!这种方法的问题是,一旦你关闭PS,你就失去了HT。“我希望我能做一次,而不必担心每次治疗。”迈克尔如阿尔罗克所说,把它放在你的个人资料里。你再也不用担心了。这也很有效,但我决定使用HT方法。不过,我确实给了你一张赞成票。我的答案适用于所有PowerShell会话,因为它是在PowerShell配置文件脚本中定义的。
Function Pro{notepad.exe$profile}
,只需键入
Pro
即可。