首页 > 解决方案 > Linux 上的 PowerShell 配置文件位置是什么?

问题描述

在 Windows 上,不包括 ISE 或 x86,有四 (4) 个配置文件脚本。

AllUsersAllHosts @ C:\Program Files\PowerShell\6\profile.ps1
AllUsersCurrentHost @ C:\Program Files\PowerShell\6\Microsoft.PowerShell_profile.ps1
CurrentUserAllHosts @ C:\Users\lit\Documents\PowerShell\profile.ps1
CurrentUserCurrentHost @ C:\Users\lit\Documents\PowerShell\Microsoft.PowerShell_profile.ps1

在带有 pwsh 6.2.0 的 Linux 上,我只能找到两个位置。

CurrentUserAllHosts @ ~/.config/powershell/Microsoft.PowerShell_profile.ps1
CurrentUserCurrentHost @ ~/.config/powershell/profile.ps1

Linux 上是否有任何“AllUsers”配置文件脚本?如果有,他们在哪里?

标签: powershellpowershell-v6.0

解决方案


tl;博士(也适用于 Windows):

  • 概念about_Profiles帮助主题描述了 PowerShell 的配置文件(初始化文件)。

  • 自动$PROFILE变量包含一个字符串,它是当前用户当前 PowerShell 主机环境(通常是终端也称为控制台)的初始化文件的路径。

  • 定义了其他配置文件 - 沿着(a)所有用户与当前用户和(b)所有主机环境与当前用户的维度 - 通过$PROFILE字符串变量装饰的属性公开,这使得它们不平凡的发现 - 见下文。

  • 默认情况下不存在任何配置文件,在某些情况下甚至它们的父目录也可能不存在;该答案的底部显示了文件的编程按需创建和更新$PROFILE


奥拉夫在评论中提供了关键指针:

$PROFILE | select *  # short for: $profile | Select-Object -Property *

显示所有配置文件位置,无论单个配置文件是否存在

例如,在我安装了 PowerShell 的 Ubuntu 机器上/home/jdoe/.powershell,我得到:

AllUsersAllHosts       : /home/jdoe/.powershell/profile.ps1
AllUsersCurrentHost    : /home/jdoe/.powershell/Microsoft.PowerShell_profile.ps1
CurrentUserAllHosts    : /home/jdoe/.config/powershell/profile.ps1
CurrentUserCurrentHost : /home/jdoe/.config/powershell/Microsoft.PowerShell_profile.ps1
Length                 : 62

请注意[string]类型的本机Length属性的存在,如果您使用它可以省略
$PROFILE | select *host*

鉴于这$PROFILE是一个字符串变量(类型[string]),您可以通过这种方式获取配置文件位置并不明显。
PowerShell使用反映所有配置文件位置[string]的成员来装饰该实例NoteProperty,这就是select( Select-Object) 能够提取它们的原因。

仅输出$PROFILE- 即字符串值- 产生/home/jdoe/.config/powershell/Microsoft.PowerShell_profile.ps1,即与其属性相同CurrentUserCurrentHost的路径,即特定于当前 PowerShell 主机环境(通常是终端又名控制台)的用户特定配置文件的路径。[1]

您可以使用反射验证这些属性的存在,如下所示(这也揭示了它们的值):

$PROFILE | Get-Member -Type NoteProperty

这意味着您还可以使用常规属性访问选项卡完成来检索个人资料位置;例如:

# Use tab-completion to find a specific profile location.
# Expands to .Length first, then cycles through the profile-location properties.
$profile.<tab>  

# Open the all-users, all-hosts profiles for editing.
# Note: Only works if the file already exists.
#       Also, you must typically run as admin to modify all-user profiles.
Invoke-Item $profile.AllUsersAllHosts

获取配置文件位置和打开配置文件进行编辑的便捷功能:

下面的代码定义:

  • Get-Profile枚举配置文件,显示它们的位置以及它们是否存在于给定的机器上。

  • Edit-Profile打开配置文件进行编辑(用于-Force按需创建);请注意,修改所有用户配置文件通常需要以管理员身份运行。

function Get-Profile {
  <#
  .SYNOPSIS
  Gets the location of PowerShell profile files and shows whether they exist.
  #>
  [CmdletBinding(PositionalBinding=$false)]
  param (
    [Parameter(Position=0)]
    [ValidateSet('AllUsersAllHosts', 'AllUsersCurrentHost', 'CurrentUserAllHosts', 'CurrentUserCurrentHost')]
    [string[]] $Scope
  )
  
  if (-not $Scope) {
    $Scope = 'AllUsersAllHosts', 'AllUsersCurrentHost', 'CurrentUserAllHosts', 'CurrentUserCurrentHost'
  }

  foreach ($thisScope in $Scope) {
    [pscustomobject] @{
      Scope = $thisScope
      FilePath = $PROFILE.$thisScope
      Exists = (Test-Path -PathType Leaf -LiteralPath $PROFILE.$thisScope)
    }
  }

}

function Edit-Profile {
  <#
  .SYNOPSIS
  Opens PowerShell profile files for editing. Add -Force to create them on demand.
  #>
  [CmdletBinding(PositionalBinding=$false, DefaultParameterSetName='Select')]
  param (
    [Parameter(Position=0, ValueFromPipelineByPropertyName, ParameterSetName='Select')]
    [ValidateSet('AllUsersAllHosts', 'AllUsersCurrentHost', 'CurrentUserAllHosts', 'CurrentUserCurrentHost')]
    [string[]] $Scope = 'CurrentUserCurrentHost'
    ,
    [Parameter(ParameterSetName='All')]
    [switch] $All
    ,
    [switch] $Force
  )
  begin {
    $scopes = New-Object Collections.Generic.List[string]
    if ($All) {
      $scopes = 'AllUsersAllHosts', 'AllUsersCurrentHost', 'CurrentUserAllHosts', 'CurrentUserCurrentHost'
    }
  }  
  process {
    if (-not $All) { $scopes.Add($Scope) }
  }

  end {
    $filePaths = foreach ($sc in $scopes) { $PROFILE.$sc }
    $extantFilePaths = foreach ($filePath in $filePaths) {
      if (-not (Test-Path -LiteralPath $filePath)) {
        if ($Force) {
          if ((New-Item -Force -Type Directory -Path (Split-Path -LiteralPath $filePath)) -and (New-Item -Force -Type File -Path $filePath)) {
              $filePath
          }
        } else {
          Write-Verbose "Skipping nonexistent profile: $filePath"
        }
      } else {
        $filePath
      }
    }
    if ($extantFilePaths.Count) {
      Write-Verbose "Opening for editing: $extantFilePaths"
      Invoke-Item -LiteralPath $extantFilePaths
    } else {
      Write-Warning "The implied or specified profile file(s) do not exist yet. To force their creation, pass -Force."
    }
  }

}

[1] PowerShell 将当前用户、当前主机配置文件视为感兴趣配置文件,这就是为什么$PROFILE字符串值包含该值。请注意,要使用注释属性来装饰[string]实例,Add-Member仅靠它是不够的;您必须使用以下成语:$decoratedString = $string | Add-Member -PassThru propName propValue- 请参阅Add-Member帮助主题


推荐阅读