英文:
PowerShell not reloading when running & $profile
问题
我更新了我的个人资料以创建/调整自定义函数,以便在完成日常工作时使用。在保存了对个人资料的编辑后,在当前的 PowerShell 会话中运行 & $profile
后,新的/更新的函数不按照新的配置文件运行。只有在关闭当前会话并重新打开一个新的 PowerShell 实例后才能正常工作。我正在使用 PowerShell 7.3.5 和 Windows 终端。
例如....
在我的个人资料中,我有一些自定义函数,用于打开下载文件夹中最近下载的 X 个文件。运行此命令 open_dnld
,不带输入将启动此文件夹中的最后一个文件。带有输入值 2 运行此命令 open_dnld(2)
将启动最近两个文件的进程。以下是函数定义:
旧函数:
Function open_dnld ([Int16] $NumFiles = 1) {foreach($file in Get-ChildItem ~/Downloads
| Sort-Object LastWriteTime -Descending
| Select-Object -First $NumFiles){
Start-Process $file
}
}
为了测试,我刚刚更新了该函数,将 $NumFiles 的默认值设置为 2,如下所示。在运行了 & $profile
之后,然后运行了 open_dnld(2)
,该函数按照先前的定义工作,并且只打开一个文件。但是,当我关闭当前的终端会话,打开一个新的 PowerShell 实例使用 Windows 终端,然后运行命令 open_dnld(2)
时,该函数现在按照新的函数定义工作。
更新的函数:
Function open_dnld ([Int16] $NumFiles = 2) {foreach($file in Get-ChildItem ~/Downloads
| Sort-Object LastWriteTime -Descending
| Select-Object -First $NumFiles){
Start-Process $file
}
}
对我做错了什么有什么想法吗?
英文:
First post in Stack Overflow so bear with me...
I update my profile to create/adjust custom functions as I complete my day-day work. After saving the edits to my profile, the new/updated function isn't working according to the new profile after running & $profile
in my current PowerShell session. It only works after closing my current session and reopening a new PowerShell instance. I am using PowerShell 7.3.5 and Windows Terminal.
For example....
In my profile I have a few custom functions to open the last X number of files downloaded to my Downloads folder. Running this command open_dnld
with no input will start the last file in this folder. Running this command with an input of 2 open_dnld(2)
starts the process for my last 2 files. Below is the function definition:
Old Function:
Function open_dnld ([Int16] $NumFiles = 1) {foreach($file in Get-ChildItem ~/Downloads
| Sort-Object LastWriteTime -Descending
| Select-Object -First $NumFiles){
Start-Process $file
}
}
For a test, I just updated the function with $NumFiles having a default value of 2 shown below. After running & $profile
and thereafter open_dnld(2)
the function works according to the previous definition and only opens 1 file. But, when I close my current terminal session, open a new instance of PowerShell using Windows Terminal, and then run the command open_dnld(2)
the function now works according to the new function definition.
Updated Function:
Function open_dnld ([Int16] $NumFiles = 2) {foreach($file in Get-ChildItem ~/Downloads
| Sort-Object LastWriteTime -Descending
| Select-Object -First $NumFiles){
Start-Process $file
}
}
Any idea on what I am doing wrong?
答案1
得分: 0
要重新加载修改过的 $PROFILE
文件 ,您必须使用 .
,即 点运算符:
. $PROFILE
相比之下,&
,即 调用运算符 在一个 子 作用域中运行脚本,这意味着其中包含的任何别名、函数等只对该脚本可见,并且在脚本结束时超出作用域。
换句话说:运行 & $PROFILE
会破坏配置文件的初衷,即在 调用者 的作用域中定义别名、函数等 - 只有 .
才能做到这一点。
详细信息请参见 此答案。
英文:
To reload a modified $PROFILE
file you must use .
, the dot-sourcing operator:
. $PROFILE
By contrast, &
, the call operator runs a script in a child scope, which means that any aliases, functions, ... it contains are visible to that script only, and go out of scope when the script ends.
In other words: running & $PROFILE
defeats the very purpose of a profile file: to define aliases, functions, ... in the caller's scope - only .
does that.
See this answer for details.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论