Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/11.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 7]_Powershell - Fatal编程技术网

启动时加载不同的PowerShell配置文件?[Powershell 7]

启动时加载不同的PowerShell配置文件?[Powershell 7],powershell,Powershell,是否有方法指定在初始化PowerShell时加载哪个PowerShell配置文件 例如,我希望能够在使用Python时加载Python Conda概要文件,或者在使用跨平台脚本时加载带有bash命令别名的概要文件 注意:我在同一台机器上,在同一个用户上。因此,特定于计算机/用户的配置文件在我的情况下不起作用。由于PowerShell配置文件只是启动PowerShell终端时加载的PowerShell脚本,因此有多种加载方式。假设您有两个配置文件: conda_profile.ps1 bash_

是否有方法指定在初始化PowerShell时加载哪个PowerShell配置文件

例如,我希望能够在使用Python时加载Python Conda概要文件,或者在使用跨平台脚本时加载带有bash命令别名的概要文件


注意:我在同一台机器上,在同一个用户上。因此,特定于计算机/用户的配置文件在我的情况下不起作用。

由于PowerShell配置文件只是启动PowerShell终端时加载的PowerShell脚本,因此有多种加载方式。假设您有两个配置文件:

  • conda_profile.ps1
  • bash_profile.ps1
直接调用配置文件脚本

您可以在打开PowerShell窗口后执行它们,例如。g、 :

.\conda_profile.ps1
通过别名调用配置文件脚本

如果名称或路径太长或太复杂,可以为它们创建别名,例如。g、 :

# Put the following two lines into the file stored in $PROFILE. You probably have to create it.
Set-Alias -Name condap -Value "C:\path\to\conda_profile.ps1"
Set-Alias -Name bashp -Value "C:\path\to\bash_profile.ps1"
之后,您可以通过键入
condap
bashp
来加载配置文件

通过快捷方式调用配置文件

您还可以创建不同的快捷方式来打开具有特定配置文件的PowerShell:

  • %APPDATA%\Microsoft\Windows\Start Menu\Programs\Windows PowerShell\Windows PowerShell.lnk复制快捷方式
  • 例如,将其重命名为
    condap
  • 将其目标从
    %SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe
    编辑到
    %SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe-ExecutionPolicy Bypass-NoExit-文件“C:\path\to\conda\u profile.ps1”
  • 分别为其他配置文件执行此操作

    启动时提示选择配置文件

    您还可以编写一个配置文件,要求您在启动PowerShell时在不同的配置文件之间进行选择。确保您拥有当前主机和当前用户的配置文件:

    if (!(Test-Path -Path $PROFILE)) {
        New-Item -Type File -Path $PROFILE -Force
    } 
    

    之后,向该文件添加一些行以请求输入,并基于该输入执行所需的概要文件脚本。这是可行的,但请记住,在大多数情况下,配置文件中的用户交互可能是一个糟糕的设计。

    您希望如何调用不同的配置文件?通过键入命令、单击快捷方式、在启动时提示选择……?您可以使用>>>关于_PowerShell_Config-PowerShell | Microsoft Docs-@Thomas Any进行自定义。我最初考虑在当前会话中运行一个命令,或者在Windows Terminal.Omg中使用
    pwsh
    的一个参数。我不相信我错过了配置文件本身就是PS脚本!如果我想在现有会话中激活它,您的选项1“通过别名调用配置文件脚本”非常完美。选项2“通过快捷方式调用配置文件”对于在Powershell中使用conda创建新的Windows终端配置文件也非常有用。