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

Linux上的PowerShell配置文件位置是什么?,powershell,powershell-v6.0,Powershell,Powershell V6.0,在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 Cu

在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
在使用PWSH6.2.0的Linux上,我只能找到两个位置

CurrentUserAllHosts @ ~/.config/powershell/Microsoft.PowerShell_profile.ps1
CurrentUserCurrentHost @ ~/.config/powershell/profile.ps1
Linux上是否有“AllUsers”配置文件脚本?如果是,它们在哪里?

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

$profile | select *  # short for: $profile | Select-Object -Property *
显示所有配置文件位置,无论单个配置文件是否存在

例如,在安装了PowerShell的Ubuntu机器上,我得到:

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]
类型的标准
长度
属性,如果使用
$profile |选择*host*
,则可以忽略该属性

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

仅输出
$profile
-即字符串值-产生
/home/jdoe/.config/powershell/Microsoft.powershell\u profile.ps1
,即与其
CurrentUserCurrentHost
属性相同的路径。[1]

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

这意味着您还可以使用常规属性访问和选项卡完成来检索各个配置文件位置;e、 g:

# 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


[1] PowerShell将当前用户、当前主机配置文件视为感兴趣的配置文件,这就是
$profile
的字符串值包含该值的原因。注意,为了用Note属性装饰
[string]
实例,仅添加成员是不够的;您必须使用以下习惯用法:
$decoratedString=$string | Add Member-PassThru-propName-propValue
-请参阅。

是否向Powershell索要它
$profile |选择对象-属性*
# 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
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."
    }
  }

}