6

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
Jamie Marshall
  • 1,885
  • 3
  • 27
  • 50
  • 1
    That's not dot sourcing it, that's launching it, like double clicking in Explorer. PS1 files default to opening in Notepad. Dot sourcing requires a dot and a space then the filename. e.g. `. .\MyScript.ps1` – TessellatingHeckler Jul 31 '17 at 03:18
  • @TessellatingHeckler Thanks. That gives me the same behavior though - still opens the file. – Jamie Marshall Jul 31 '17 at 03:22
  • @JamieMarshall: Nope. Dot sourcing will never ever do that. If the Myscript.ps1 is having any function inside that, then simply dot source and call the function. see if the output is coming or not – Ranadip Dutta Jul 31 '17 at 03:51
  • how exactly are you running that code? Does it do that with every psm1 or ps1 you dot source, or only those two? Even empty files? If you open powershell and type `echo 1 > test.ps1; . .\test.ps1` does it open notepad? – TessellatingHeckler Jul 31 '17 at 05:54
  • 1
    @JamieMarshall from your code snippet in ex2 ,it looks like you are trying to dot source .psm1. Can you try renaming the files as .ps1 and give it a try ? – ClumsyPuffin Jul 31 '17 at 06:37

3 Answers3

7

You cannot dot source PowerShell files with the .psm1 file extension. One option is to rename them to .ps1.

Alternatively (and, in my opinion the better approach), you can load the PowerShell modules using Import-Module <module.psm1>. Just note that the behavior of Import-Module is different from dot sourcing it. Dot sourcing runs the script in the current scope and also persists all variables, functions, etc.in the current scope. Import-Module does not do that.

Although not very common, you can also export variables from modules with Export-ModuleMember.

Thomas Glaser
  • 1,670
  • 1
  • 18
  • 26
  • But sometimes we need to unit test the module. Some of the tests are for an internal function.I found that I couldn't import psm1 during the unit test.And the first thing I thought of was to look at the official documents, but I didn't find the relevant content in the official documents.At present, my strategy is to clone the file with suffix PS1, then import it, and delete it after testing. Maybe there is a better way to achieve it. – Andy Jan 24 '21 at 03:56
1

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.

Carl Walsh
  • 6,100
  • 2
  • 46
  • 50
0

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.

Andy
  • 1,077
  • 1
  • 8
  • 20