Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

PowerShell not reloading when running & $profile

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….

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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?

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading