windows 如何设置 PowerShell 的默认目录?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32069265/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do you set PowerShell's default directory?
提问by Youbloodywombat
Is there a way to change PowerShell's default location?
有没有办法更改 PowerShell 的默认位置?
How do you set PowerShell's default working directory?
如何设置 PowerShell 的默认工作目录?
采纳答案by Peter Hahndorf
回答by Jeremy Danyow
Create a PowerShell profileas follows.
如下创建PowerShell 配置文件。
Run PowerShell as administrator and execute the following command:
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned
This will permit PowerShell to run local scripts and scripts downloaded from the Internet that have been signed. Read more about this command in the documentation.
In your
Documents
folder, find a folder namedWindowsPowerShell
for classic PowerShell orPowerShell
for newer PowerShell Core. If it does not exist, that's ok; just create it.- Create a new file named
profile.ps1
in theWindowsPowerShell
folder (orPowerShell
for PowerShell Core). Open
profile.ps1
and add the following command to set your default working directory:Set-Location C:\my\default\working\directory
Open a new PowerShell window... the changes should have taken effect.
以管理员身份运行 PowerShell 并执行以下命令:
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned
这将允许 PowerShell 运行已签名的本地脚本和从 Internet 下载的脚本。在文档中阅读有关此命令的更多信息。
在您的
Documents
文件夹中,找到一个以WindowsPowerShell
经典 PowerShell 或PowerShell
更新的 PowerShell Core命名的文件夹。如果它不存在,那没关系;只需创建它。- 创建一个
profile.ps1
在WindowsPowerShell
文件夹中命名的新文件(或PowerShell
用于 PowerShell Core)。 打开
profile.ps1
并添加以下命令以设置默认工作目录:Set-Location C:\my\default\working\directory
打开一个新的 PowerShell 窗口...更改应该已经生效。
回答by neongrau
I had tried the above answers in Windows Server 2016without success.
我曾在Windows Server 2016 中尝试过上述答案,但没有成功。
But I found this approach (it should be the same for Windows 10) working for me.
但是我发现这种方法(对于 Windows 10 应该是相同的)对我有用。
- Start a PowerShell session
- In the Taskbar, right-click and pin to keep a link there
- Again right click the icon in taskbar and then right-click Windows PowerShelland choose Properties
- Enter your preferred directory in the Start in:input field and press OK
- Start from the taskbar icon
- 启动 PowerShell 会话
- 在任务栏中,右键单击并固定以在那里保留链接
- 再次右键单击任务栏中的图标,然后右键单击Windows PowerShell并选择属性
- 在开始于:输入字段中输入您的首选目录,然后按确定
- 从任务栏图标开始
Done!
完毕!
In the same Propertiesdialog you can also change many other settings like fonts, colors, sizes and on the Shortcuttab there via button Advanced. You can select if that PowerShell session is to be run with administrator privileges.
在同一个属性对话框中,您还可以通过按钮更改许多其他设置,如字体、颜色、大小和快捷选项卡上的设置Advanced。您可以选择是否以管理员权限运行该 PowerShell 会话。
回答by Antonio
An easier way to set the default directory is the following:
设置默认目录的更简单方法如下:
- Right click the Windows PowerShell icon and pin to Start
Right click the Windows PowerShell icon in Start, and again right click Windows PowerShell and select Properties (not Run as Administratorand not Windows PowerShell ISE)
- In the Shortcuttab -> 'Start in' field, change to the location you want PowerShell to start in.
回答by rochester
Type this in PowerShell:
在 PowerShell 中输入:
New-Item -path $profile -type file –force
It creates a .ps1 file in the PowerShell folder. Open it, and edit it as:
它会在 PowerShell 文件夹中创建一个 .ps1 文件。打开它,并将其编辑为:
Set-location C:\files
Done
完毕
Refer to this link. It works fine.
请参阅此链接。它工作正常。
回答by Leonard Brünings
Putting Set-Location
into your profile will unconditionally change the current working directory, which might have unwanted consequences in regards to the working directory for scripts that you execute via "run with PowerShell".
把Set-Location
到您的个人资料将无条件改变当前的工作目录,这可能有关于工作目录不必要的后果脚本,您通过“使用PowerShell运行”执行。
An alternative solution is to change the working directory for the .lnk
files to PowerShell usually found in %USERPROFILE%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Windows PowerShell
. Right click on a link, and change the working directory from %HOMEDRIVE%%HOMEPATH%
to the directory you want.
另一种解决方案是将.lnk
文件的工作目录更改为通常在%USERPROFILE%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Windows PowerShell
. 右键单击一个链接,然后将工作目录从更改为%HOMEDRIVE%%HOMEPATH%
您想要的目录。
回答by Sundhar
Instead of unconditionally changing the working directory as mentioned in previous answers, you can write a simple function in the PowerShell profile to use Set-Location
to quickly change the working directory whenever necessary.
您可以在 PowerShell 配置文件中编写一个简单的函数,以便Set-Location
在必要时快速更改工作目录,而不是像前面的答案中提到的那样无条件地更改工作目录。
Check Jeremy Danyow's answerto create/modify a PowerShell profile.
检查Jeremy Danyow 的答案以创建/修改 PowerShell 配置文件。
Add a function(s) to your PowerShell profile:
将函数添加到您的 PowerShell 配置文件:
function goto_this {set-location 'your\path\to\some\dir'}
function goto_that {set-location 'your\path to some\dir with space'}
Just change the function name and directory pointed to. Using quotes on the path is mandatory if it contains spaces. I try to keep the prefix goto_
as it helps in remembering the functions' names.
只需更改指向的函数名称和目录即可。如果路径包含空格,则必须在路径上使用引号。我尽量保留前缀,goto_
因为它有助于记住函数的名称。
You can start typing goto_
then press TAB to cycle through all the added functions (remember to start a new PowerShell window after adding/modifying functions).
您可以开始输入goto_
然后按 TAB 循环浏览所有添加的功能(请记住在添加/修改功能后启动一个新的 PowerShell 窗口)。
回答by Luke Angel
Using just the command line, if a file exists already it will append to it:
仅使用命令行,如果文件已经存在,它将附加到它:
$(if (-Not (Test-Path ~\Documents\WindowsPowerShell\)){ mkdir ~\Documents\WindowsPowerShell\}) ; echo "Set-Location c:\THELOCATIONYOUWANT" >> ~\Documents\WindowsPowerShell\profile.ps1
回答by RangerGeof
With that, there seems to be some confusion on the "working directory" and PowerShell's "location". What most people here are doing, and saying to do is change PowerShell's "location". The "working directory" is actually different. Here is an articlethat explains it.
有了这个,“工作目录”和PowerShell的“位置”似乎有些混乱。这里的大多数人正在做的,并说要做的是更改 PowerShell 的“位置”。“工作目录”实际上是不同的。这是一篇解释它的文章。
For those who don't want to read the article: Open PowerShell and use what others have said to do Set-Location "C:\some\directory"
. Notice that your "working directory" is still where your PowerShell was opened at. Either "~"
or "%SYSTEMROOT%\system32"
depending on if you ran as administrator or not. To check the working directory, use [Environment]::CurrentDirectory
.
对于那些不想阅读文章的人:打开 PowerShell 并使用其他人所说的去做Set-Location "C:\some\directory"
。请注意,您的“工作目录”仍然是打开 PowerShell 的位置。无论是"~"
或"%SYSTEMROOT%\system32"
取决于如果你跑了作为管理员或没有。要检查工作目录,请使用[Environment]::CurrentDirectory
.
Note:in the article the author says to check the "working directory" by using this command:
注意:在文章中作者说使用以下命令检查“工作目录”:
\[Environment\]::CurrentDirectory
I am not sure if this works with older PowerShell versions, but with PowerShell 5 (and later) you have to use [Environment]::CurrentDirectory
.
我不确定这是否适用于较旧的 PowerShell 版本,但对于 PowerShell 5(及更高版本),您必须使用[Environment]::CurrentDirectory
.
回答by tedro
Make this the first line in your Profile.ps1 and PowerShell Core (pwsh) will open in the directory you are currently working in:
将此作为 Profile.ps1 中的第一行,PowerShell Core (pwsh) 将在您当前工作的目录中打开:
set-location (get-location).path
设置位置(get-location)。路径