Powershell $PROFILE 变量指向错误的位置。它设置的 $PROFILE 变量在哪里?

Powershell $PROFILE variable pointing to wrong location. Where is $PROFILE variable it set?

对我的机器进行一些更改后,Powershell 失败,因为 $PROFILE 变量指向与之前不同的位置,即它被设置为

C:\Powershell\Microsoft.PowerShell_profile.ps1

而不是

C:\Users\user\Documents\Powershell\Microsoft.PowerShell_profile.ps1

这会导致诸如找不到已安装模块等问题。所以我的问题是 Powershell 如何设置 $PROFILE 的值?可以改吗?

已解决。原来在问题发生之前,$PROFILE指向的是OneDrive中的Documents文件夹,即

C:\Users\user\OneDrive\Documents\PowerShell\Microsoft.PowerShell_profile.ps1

当我启用文档备份到 OneDrive 并重新登录时,$PROFILE 回到了正确的位置。似乎如果 Powershell 在 Documents 或 $HOME 下找不到配置文件位置,它默认为路径 C:\Powershell。但是我不确定 - 如果对这种行为有明确的说明会很好。

我在寻找答案时遇到了这个问题。

要更改 Powershell 查找默认配置文件的位置,您需要更改注册表项。

请注意,配置文件将在新目录下找到 WindowsPowerShell/Microsoft.PowerShell_profile.ps1

所需路径:C:/Users/admin Powershell 配置文件:C:/Users/admin/WindowsPowerShell/Microsoft.PowerShell_profile.ps1


手动方式:

打开注册表编辑器并导航至:

Computer\HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders

Personal的值改为您选择的目录。


使用 powershell 的自动化方式:

Set-ItemProperty -Path "HKCU:\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders" -Name "Personal" -Value "DRIVEHERE"

然后您可以测试设置的值:

Get-ItemProperty -Path "HKCU:\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders" -Name "Personal"