如何在PowerShell中显示当前目录?


22

我想在powershell命令行上显示当前的工作目录,就像在(例如)CMD提示符下那样。我该怎么办?

Answers:


22

检查一下:http : //mshforfun.blogspot.com/2006/05/perfect-prompt-for-windows-powershell.html

基本上,您可以创建一个名为“ Microsoft.PowerShell_profile.ps1”的“配置文件”文件,该文件将在每次启动Powershell时运行。

根据要运行该文件的对象,可以将几个文件放入该文件夹(在上面的链接中有解释)。如果仅是您自己,则可以在“我的文档”文件夹中创建一个名为WindowsPowerShell的文件夹,并将其放在该文件夹中。

如果将此功能放在该文件中:

function prompt
{
    "PS " + $(get-location) + "> "
}

它将使您的提示如下所示:

PS C:\directory\path\here>

您可以放很多其他东西,但这是基础。

注意:在使用配置文件脚本之前,您需要从Powershell运行“ set-executionpolicy remotesigned”-这将允许您运行在计算机上本地编写的未签名脚本以及其他计算机上的已签名脚本。


似乎没有为Visual Studio的外接:(哦工作。
比利·奥尼尔

@BillyONeal:检查的价值$profile.CurrentUserCurrentHost$profile.CurrentUserCurrentHost看什么(用户)配置文件脚本是有效的。不同的主机(例如VS vs. ISE)对于$profile.CurrentUserCurrentHost
Richard

9

很简单,将以下内容添加到您的profile.ps1文件中(在“ 我的文档” \ WindowsPowerShell文件夹下):

功能提示{“ $ pwd>”}


2

如今,这可以正常工作:

echo "$PWD"

它的工作原理不同

echo $PWD

只是不要忘记引号:)下面的示例输出。

PS C:\Users\user name> echo $PWD

Path
----
C:\Users\user name


PS C:\Users\user name> echo "$PWD"
C:\Users\user name
PS C:\Users\user name>

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.