Windows Powershell $配置文件不显示真实路径

Mar*_*ark 18 powershell windows-10

我启动Windows Powershell(通过按下Windows键,键入"powershell"并按下启动的输入C:\Windows\System32\WindowsPowerShell\v1.0)并键入$profile并按Enter键并查看WindowsPowerShell\Microsoft.PowerShell_profile.ps1

据我所知,这不是一条有效的道路.我希望有类似的东西C:\Windows\...

$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    : WindowsPowerShell\profile.ps1
CurrentUserCurrentHost : WindowsPowerShell\Microsoft.PowerShell_profile.ps1
Length                 : 50
Run Code Online (Sandbox Code Playgroud)

然而CurrentUserAllHosts,CurrentUserCurrentHosts仍然是非路径.这些非路径是什么意思?它们是指某些隐藏的值还是我需要在某处设置一些系统值?

这是我的 $PsVersionTable.PsVersion

Major  Minor  Build  Revision
-----  -----  -----  --------
5      1      14393  206
Run Code Online (Sandbox Code Playgroud)

这是结果 Get-Host

Name             : ConsoleHost
Version          : 5.1.14393.206
InstanceId       : a2a61a42-f2ee-46b9-b67a-ef441301bdb8
UI               : System.Management.Automation.Internal.Host.InternalHostUserInterface
CurrentCulture   : en-US
CurrentUICulture : en-US
PrivateData      : Microsoft.PowerShell.ConsoleHost+ConsoleColorProxy
DebuggerEnabled  : True
IsRunspacePushed : False
Runspace         : System.Management.Automation.Runspaces.LocalRunspace
Run Code Online (Sandbox Code Playgroud)

mkl*_*nt0 12

tl;博士:

问题可能与PowerShell无关,但可能是由于注册表中缺少特殊文件夹路径定义.

验证注册表项是否HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders包含以数据REG_EXPAND_SZ命名的值- 请参阅下面的诊断命令.Personal%USERPROFILE%\Documents

如果您发现必须(重新)创建它,请使用:

New-ItemProperty `
  'HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders' `
  Personal -Value '%USERPROFILE%\Documents' -Type ExpandString -Force
Run Code Online (Sandbox Code Playgroud)

然后注销并重新启动(或重新启动)以查看是否解决了问题.


Eris的有用答案告诉我们,用户特定的 PS配置文件路径是从Environment.GetFolderPath(Environment.SpecialFolder.Personal)返回的内容中获得的.

.NET可能通过SHGetKnownFolderPathShell API函数从注册表项HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\User Shell Foldersvalue 获取此值,Personal其中它通常被定义为REG_EXPAND_SZ包含(可扩展)字符串的值%USERPROFILE%\Documents.
(还有一个传统的后备位置 - 请看这里.)

配置文件CurrentUserAllHostsCurrentUserCurrentHost仅包含相对路径,

  • WindowsPowerShell\profile.ps1
  • WindowsPowerShell\Microsoft.PowerShell_profile.ps1

建议将其结果用作路径前缀Environment.GetFolderPath(Environment.SpecialFolder.Personal)调用意外返回一个空字符串,这反过来表明存在注册表问题.


以下是一些诊断命令及其预期输出(jdoe代表您的用户名):

# Verify that %USERPROFILE% is defined.
> $env:USERPROFILE
C:\Users\jdoe

# Verify that %USERPROFILE% is defined in the registry.
> Get-ItemPropertyValue 'HKCU:\Volatile Environment' USERPROFILE
C:\Users\jdoe

# Verify that the API call to retrieve the known folder
# "Personal" (Documents) returns the expected value.
> [Environment]::GetFolderPath('Personal')
C:\Users\jdoe\Documents

# See if the registry contains the expected definition.
> Get-ItemPropertyValue 'HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders' Personal
C:\Users\jdoe\Documents
Run Code Online (Sandbox Code Playgroud)


Eri*_*ris 8

根据github上的powershell源代码,他们寻找 Environment.SpecialFolder.Personal

它从ConsoleHost.cs开始, 你可以跟踪它们调用的utils.csEnvironment.GetFolderPath(Environment.SpecialFolder.Personal);