首页 > 解决方案 > 如何使非高架外壳的 PowerShell 功能永久化?

问题描述

我希望每次打开任何 PowerShell 终端(包括非提升!)时都加载一个 PowerShell 函数。我尝试了以下方法:

  1. mkdir "$env:USERPROFILE\Documents\WindowsPowerShell\Functions"
  2. 将我的函数作为 *.ps1-files 添加到目录
  3. 打开 $profile ( if (!(Test-Path (Split-Path $profile))) { mkdir (Split-Path $profile) } ; if (!(Test-Path $profile)) { New-Item $profile -ItemType file } ; notepad $profile)
  4. 将此添加到配置文件中:
# Load own custom functions at startup
$OwnFunctionsDir = "$env:USERPROFILE\Documents\WindowsPowerShell\Functions"
Write-Host "Loading own PowerShell functions from:" -ForegroundColor Green
Write-Host "$OwnFunctionsDir" -ForegroundColor Yellow
Get-ChildItem "$OwnFunctionsDir\*.ps1" | %{.$_}
Write-Host ''

它适用于高架外壳,但对于非高架外壳,它不会加载任何东西。我怎样才能使它与非高架壳一起工作?

高架 非高架
在此处输入图像描述 在此处输入图像描述

标签: windowspowershell

解决方案


问题是我的“我的文档”路径设置不正确。这就是我修复它的方法:

  1. 打开regedit.exe
  2. 浏览到以下路径:HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders
  3. 查找注册表项Personal
  4. 将其值更改为%USERPROFILE%\Documents
  5. 重新启动计算机
  6. 从问题中重复步骤 3. 和 4.

推荐阅读