Powershell dot sourcing opens up file in notepad

Viewed 2307

Everytime i dot source a file in PowerShell it opens a copy of the file in notepad.

Exe:

.\MyScript.ps1

The script runs fine - its just really annoying having these pop up all the time. Is there a way to suppress this?

I'm on windows 7 x64 and using the latest version of PowerShell.

Ex2: This is still launching notepad.

cls

Set-Location "\\PSCWEBP00129\uploadedFiles\psDashboard\"
. .\assets\DCMPull\Powershell\SqlServerTransfer.psm1
. .\assets\DCMPull\Powershell\RunLogging.psm1
3 Answers

Adding to Raziel's answer, there's a lot of thought that went into only being able to dot source files with .ps1 extension, and otherwise why it tries to run it as a system executable. Here's a snippet from PeterWhittaker on GitHub:

. ./afile would only execute something if there's either an extension-less but executable aFile in the current dir, or a (not-required-to-be-executable) afile.ps1 file, with the former taking precedence if both are present; if the file exists, but is neither executable nor has extension .ps1, it is opened as if it were a document.

. <filename> with <filename> being a mere name (no path component) by (security-minded) design only ever looks for a file of that name in the directories listed in $env:PATH (see below), not in the current directory.

I encountered exactly the same situation : If the point source imports the .psm1 file, the file will be opened directly instead of importing the code in the file.

Because the function of point source import is only valid in the file with suffix of.ps1, if the suffix does not meet the requirements, it will not be regarded as path, but as a code , so it is like running the corresponding string directly, and the effect is naturally to open the file.

So,this phenomenon is not aimed at .PSM1,if you change the extension to TXT, it will have the same effect. It will have the same effect for any file whose suffix is not .PS1.

You can bypass this problem by creating symbolic links or hard links!

In PowerShell 7, it's easy to create links using New-Item.

Related