PowerShell 配置文件未加载

Whi*_*ane 10 powershell

我创建了文件C:\Users\<myname>\Documents\WindowsPowerShell\profile.ps1;但是,PowerShell 不会在启动时加载它。我已经测试了默认的 PowerShell 以及 VS Code 的集成 PowerShell。我也尝试将profile.ps1文件重命名为Microsoft.PowerShell_profile.ps1,但这没有任何改变。每次更改后,我都会重新启动有问题的应用程序。

运行Test-Path $profile.CurrentUserAllHosts返回 True。

配置文件当前仅包含一行 ( Set-PSReadlineKeyHandler -Key Tab -Function Complete)。启动 PowerShell 或 VS Code 的集成 shell 后,运行Get-PSReadlineKeyHandler | findstr -i Tab显示尚未设置。复制并粘贴配置文件中的行,运行它,然后Get-...再次运行该命令显示它已正确设置,并且更改做了它应该做的。重新启动外壳将恢复为默认设置。

我是否还需要执行其他步骤才能让 PowerShell 执行配置文件?(我来自 Linux/UNIX 背景,我认为 PowerShell 的工作方式类似,但当然 PowerShell 的设计完全不同)。如果没有额外的步骤,为什么 PowerShell 在这种情况下不加载配置文件?

附加信息:

> $profile | Format-List * -force
AllUsersAllHosts       : C:\Windows\System32\WindowsPowerShell\v1.0\profile.ps1
AllUsersCurrentHost    : C:\Windows\System32\WindowsPowerShell\v1.0\Microsoft.PowerShell_profile.ps1
CurrentUserAllHosts    : C:\Users\<myname>\Documents\WindowsPowerShell\profile.ps1
CurrentUserCurrentHost : C:\Users\<myname>\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1
Length                 : 76
Run Code Online (Sandbox Code Playgroud)

Whi*_*ane 13

解决方案:更改执行策略

  1. 以管理员身份启动 PowerShell
  2. Set-ExecutionPolicy -ExecutionPolicy Unrestricted

错误信息如图所示:

. : File C:\Users\<myname>\Documents\WindowsPowerShell\profile.ps1 cannot be loaded because running scripts is disabled on this system. For more information, see about_Execution_Policies at http://go.microsoft.com/fwlink/?LinkID=135170.
At line:1 char:3
+ . 'C:\Users\<myname>\Documents\WindowsPowerShell\profile.ps1'
+   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : SecurityError: (:) [], PSSecurityException
    + FullyQualifiedErrorId : UnauthorizedAccess
Run Code Online (Sandbox Code Playgroud)

课程:

奇怪的是,我之前每次启动 PS 时都会看到一条错误消息(这并不重要),所以我只是习惯了忽略它。创建profile.ps1文件引起出现在执行政策的错误,但也造成了以前的错误,以阻止出现,所以我就跳过它,因为,不读的细节,它只是看起来像红色文字的同一个街区。(由于某种原因,以前的错误现在似乎无法复制。)

最终,问题可以描述为“用户习惯了忽略琐碎和不重要的错误,所以忽略了重要的错误”。

  • 这对我不起作用。我什至注销并重新登录,然后执行返回“Unrestricted”的“get-executionpolicy”。然而,shell 仍然没有运行我的 profile.ps1 文件。 (2认同)