Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powershell dot sourcing opens up file in notepad

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
like image 868
Jamie Marshall Avatar asked Jul 31 '17 02:07

Jamie Marshall


2 Answers

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.

like image 100
Thomas Glaser Avatar answered Nov 20 '22 04:11

Thomas Glaser


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.

like image 1
Carl Walsh Avatar answered Nov 20 '22 03:11

Carl Walsh