Linux 上的 PowerShell 配置文件位置是什么?

What are PowerShell profile locations on Linux?

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

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

在 Linux 上使用 pwsh 6.2.0 我只能找到两个位置。

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

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

tl;dr(也适用于Windows):

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

  • automatic $PROFILE variable包含一个字符串,它是当前用户[=115=的初始化文件的路径] 和 当前的 PowerShell 主机环境 (通常是终端 a.k.a 控制台)。

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

  • None 配置文件存在 默认情况下 ,在某些情况下甚至 父目录 可能不会; 的底部显示了 $PROFILE 文件的程序化按需创建和更新。


Olaf 在评论中提供了关键指针:

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

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

例如,在我的 Ubuntu 机器上 /home/jdoe/.powershell 安装了 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 变量(类型 [string]),您可以通过这种方式获取配置文件位置并不明显。
PowerShell 用反映所有配置文件位置的 NoteProperty 成员装饰 [string] 实例,这就是 select (Select-Object) 能够提取它们的原因。

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

您可以通过反射验证这些属性是否存在,(这也揭示了它们的值):

$PROFILE | Get-Member -Type NoteProperty

这意味着您还可以使用常规属性访问tab补全来检索个人个人资料位置;例如:

# 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 help topic.