如何永久将PowerShell的$PROFILE改为指向本地Documents文件夹而非OneDrive?
永久修改PowerShell $PROFILE路径到本地文件夹
问题背景
升级到Windows 11后,PowerShell的$PROFILE环境变量默认指向OneDrive同步的Documents文件夹:
PS> echo $PROFILE C:\Users\<user>\OneDrive\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1
希望将配置文件路径改为本地Documents文件夹下的指定位置:
$env:UserProfile\Documents\PowerShell\Microsoft.PowerShell_profile.ps1
永久修改方法
方法一:调整系统文档库默认位置(根源解决)
PowerShell的$PROFILE路径依赖于系统「文档」库的默认保存位置,Win11默认将其同步到OneDrive,修改步骤如下:
- 打开文件资源管理器,右键左侧栏的「文档」→ 选择「属性」
- 切换到「位置」标签页,点击「移动」,选择本地路径
C:\Users\<你的用户名>\Documents,点击「选择文件夹」 - 点击「应用」,根据需求选择是否移动现有文件(不想同步的话选「否」)
- 重启PowerShell,运行
echo $PROFILE即可看到路径已更新为本地文件夹
方法二:通过注册表指定自定义配置路径(单独控制PowerShell)
如果不想修改系统文档库的全局位置,可直接通过注册表设置PowerShell的配置文件路径:
- 创建本地目标文件夹和配置文件:
New-Item -Path "$env:UserProfile\Documents\PowerShell" -ItemType Directory -Force New-Item -Path "$env:UserProfile\Documents\PowerShell\Microsoft.PowerShell_profile.ps1" -ItemType File -Force
- 以管理员身份打开PowerShell,运行以下命令修改注册表:
# 确保执行策略允许加载本地配置文件 Set-ItemProperty -Path "HKCU:\Software\Microsoft\PowerShell\1\ShellIds\Microsoft.PowerShell" -Name "ExecutionPolicy" -Value "RemoteSigned" # 设置自定义配置文件路径 Set-ItemProperty -Path "HKCU:\Software\Microsoft\PowerShell\1\ShellIds\Microsoft.PowerShell" -Name "Profile" -Value "$env:UserProfile\Documents\PowerShell\Microsoft.PowerShell_profile.ps1"
- 重启PowerShell,运行
echo $PROFILE验证路径已修改
验证
重启PowerShell后,执行以下命令确认配置文件路径和文件存在:
echo $PROFILE Test-Path $PROFILE
内容的提问来源于stack exchange,提问作者eclipse
相关产品推荐
相关产品推荐

