如何从PowerShell中的脚本文件重新加载用户配置文件 [英] How to reload user profile from script file in PowerShell
问题描述
我想从脚本文件中重新加载我的用户配置文件.我认为从脚本文件中进行点源化可以解决问题,但是不起作用:
I want to reload my user profile from a script file. I thought that dot sourcing it from within the script file would do the trick, but it doesn't work:
# file.ps1
. $PROFILE
但是,如果我从PowerShell的解释器中获取源代码,它确实可以工作.
However, it does work if I dot source it from PowerShell's interpreter.
我为什么要这样做?
每次更新配置文件并进行测试时,我都会运行此脚本,因此,我避免避免重新启动PowerShell来刷新环境.
I run this script every time I update my profile and want to test it, so I'd like to avoid having to restart PowerShell to refresh the environment.
推荐答案
因此,您标记为答案的方法可能在Powershell命令提示符下有效,但在PowerShell ISE中无效(对于我来说,提供了出色的PowerShell会话),可能无法在其他PowerShell环境中正常工作.
So, the approach that you marked as the answer may work inside the Powershell command prompt, but it doesn't work inside PowerShell ISE (which, to me, provides a superior PowerShell session) and probably won't work right in other PowerShell environments.
这是一个我已经使用了一段时间的脚本,它在任何环境下都对我非常有效.我只是将此函数放在〜\ Documents \ WindowsPowerShell中的Profile.ps1中,每当我要重新加载我的概要文件时,我都会将该函数点源化,即
Here's a script that I have been using for a while, and it has worked very well for me in every environment. I simply put this function into my Profile.ps1 at ~\Documents\WindowsPowerShell, and whenever I want to reload my profile, I dot-source the function, i.e.
. Reload-Profile
功能如下:
function Reload-Profile {
@(
$Profile.AllUsersAllHosts,
$Profile.AllUsersCurrentHost,
$Profile.CurrentUserAllHosts,
$Profile.CurrentUserCurrentHost
) | % {
if(Test-Path $_){
Write-Verbose "Running $_"
. $_
}
}
}
这篇关于如何从PowerShell中的脚本文件重新加载用户配置文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!