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
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With