Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powershell Module not loading even after the module path is defined in environment variables

I have a module in a folder that I have created, C:\PowerShellScripts\Modules. I call this mod.psm1. This file contains two simple functions: Write-hello and Write-bye.

Now, I have added this path(C:\PowerShellScripts\Modules) to my PSModulePath Environment variables path.

Accordingly, this should auto load the module when I start PowerShell. Correct?

But when I run PowerShell and try Write-hello or Write-bye it gives an error

write-hello : The term 'write-hello' is not recognized as the name of a cmdlet, function, script file, or operable program

PowerShell does not load the mod.psm1 file even though $ENV:PSModulePath shows my path (C:\PowerShellScripts\Modules)

And I need to manually use Import-Module again. What could be wrong?

like image 230
Kiran6699 Avatar asked Apr 19 '14 07:04

Kiran6699


People also ask

How do I enable a PowerShell module installed?

You should open the PowerShell prompt as the Administrator to install the PSWindowsUpdate module. Enter Set-ExecutionPolicy -ExecutionPolicy RemoteSigned to enable remote script execution. Enter Install-Module -Name PSWindowUpdate -Force to download and install the PSWindowUpdate module.

How do I change the path of a PowerShell module?

To add or change files in the $env:Windir\System32 directory, start PowerShell with the Run as administrator option. You can change the default module locations on your system by changing the value of the PSModulePath environment variable, $Env:PSModulePath .

What is AllowClobber in PowerShell?

-AllowClobberIndicates that this cmdlet imports the specified commands, even if they have the same names as commands in the current session. If you import a command with the same name as a command in the current session, the imported command hides or replaces the original commands.


2 Answers

According to documentation available here:

A "well-formed" module is a module that is stored in a directory that has the same name as the base name of at least one file in the module directory. If a module is not well-formed, Windows PowerShell does not recognize it as a module.

The "base name" of a file is the name without the file name extension. In a well-formed module, the name of the directory that contains the module files must match the base name of at least one file in the module.

This is the reason when you made a directory named mod (same base name as the file mod.psm1) and put the module inside it, you could access your cmdlets.

like image 146
Nitesh Avatar answered Sep 27 '22 21:09

Nitesh


I figured it out. I needed to add my mod.psm1 file to a folder named "mod". Now I can directly access my cmdlets write-hello or write-bye when i start PowerShell.

like image 22
Kiran6699 Avatar answered Sep 27 '22 21:09

Kiran6699