解释一下 PowerShell 配置文件。

powershellmicrosoft technologiessoftware & coding更新于 2026/1/10 19:07:17

打开 PowerShell 时,它会像 Windows 操作系统一样加载配置文件。登录 Windows 操作系统后,您将登录到您的配置文件,每个用户都有其各自的配置文件。它被称为当前主机的当前配置文件。

要检查您的配置文件,请在 PowerShell 控制台中输入 $Profile 命令。

PS C:\Users\Administrator> $profile
C:\Users\Administrator\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.p
s1

这是针对 Powershell 控制台的,但让我们检查一下 Powershell 是否对 ISE 使用相同的配置文件。

PS C:\> $profile
C:\Users\Administrator\Documents\WindowsPowerShell\Microsoft.PowerShellISE_profile
e.ps1

因此,ISE 也有自己的配置文件,并且都存储在 $Home 目录中。如果我们将 $ 配置文件用于 VSCode,会怎么样呢?

PS C:\> $profile
C:\Users\Administrator\Documents\PowerShell\Microsoft.VSCode_profile.ps1

这意味着每个编辑器都有针对当前用户和当前主机的配置文件。

您可能已经注意到,每当启动 PowerShell 时,您都可以从模块路​​径访问系统上不同用户创建的命令和模块,因为启动 PowerShell 也会加载存储在 $PSHome 位置的模块。除了当前用户之外

PS C:\> $pshome
C:\Windows\System32\WindowsPowerShell\v1.0

以上示例表明,也可能存在一个适用于所有用户的配置文件。让我们看看 PowerShell ISE 版本总共有多少个配置文件。

PS C:\> $profile | fl * -Force
AllUsersAllHosts          : C:\Windows\System32\WindowsPowerShell\v1.0\profile.ps1
AllUsersCurrentHost       : C:\Windows\System32\WindowsPowerShell\v1.0\Microsoft.Pow
erShellISE_profile.ps1
CurrentUserAllHosts       : C:\Users\Administrator\Documents\WindowsPowerShell\profi
le.ps1
CurrentUserCurrentHost    : C:\Users\Administrator\Documents\WindowsPowerShell\Micro
soft.PowerShellISE_profile.ps1
Length                    : 86

上述命令是从 ISE 执行的,我们现在将在 PowerShell 控制台中检查相同的命令,

PS C:\Users\Administrator> $PROFILE | fl * -Force
AllUsersAllHosts          : C:\Windows\System32\WindowsPowerShell\v1.0\profile.ps1
AllUsersCurrentHost       : C:\Windows\System32\WindowsPowerShell\v1.0\Microsoft.Pow
erShell_profile.ps1
CurrentUserAllHosts       : C:\Users\Administrator\Documents\WindowsPowerShell\profi
le.ps1
CurrentUserCurrentHost    : C:\Users\Administrator\Documents\WindowsPowerShell\Micro
soft.PowerShell_profile.ps1
Length                    : 83

比较以上两个输出,您会注意到当前主机(所有用户和当前用户)配置文件取决于您使用的编辑器。如果您使用 PowerShell 控制台,配置文件名称将包含 PowerShell 配置文件;如果您使用 ISE 或 VSCode,当前主机配置文件将包含相应的名称。

通过比较,我们发现配置文件主要存储在两个位置。1) $Home (C:\Users\<UserName>) 和 2) $PSHome (C:\Windows\System32\WindowsPowerShell)。

因此总共有 6 个配置文件。

  • 当前用户,当前主机 – PowerShell 控制台

  • 当前用户,所有主机

  • 所有用户,当前主机 - PowerShell 控制台

  • 所有用户,所有主机

  • 当前用户,当前主机 - 取决于编辑器

  • 所有用户,当前主机 - 取决于编辑器。


相关文章