0

I am trying to load PowerShell Script Module. Below is the step I am following

I have created 2 files under folder "C:\windows\system32\WindowsPowerShell\v1.0\Modules**PSModuleTest**"

PSModuleTest.psd1

@{
ModuleVersion = '1.0'
GUID = '7e8f93e6-5bde-4043-918e-322066c5340e'
Author = 'ravi'
CompanyName = 'Unknown'
Copyright = '(c) 2016 Ravi. All rights reserved.'
FunctionsToExport = '*'
CmdletsToExport = '*'
VariablesToExport = '*'
AliasesToExport = '*'
}

PSModuleTest.psm1

function ModuleTestFunction
{
    Write-Host "Hello world!!!"
}

Once above two files are placed under correct folder I have tested whether the module path is included in $env:PSModulePath or not and its there. I also verified that I am not on older version of PowerShell by entering $host command.

When I executed Get-Module command, surprizingly i dont see my new module loaded. So I tried to load the module by entering below command and it did not worked.

Import-Module -Name PSModuleTest -Force -Verbose

See below image for all the command's output for more details.

enter image description here

1 Answer 1

1

You can see from your screenshot above that after you've imported it PSModuleTest has no exported commands.

You need to declare PSModuleTest.psm1 in your module manifest file as long as you have a manifest available. e.g.

# Script module or binary module file associated with this manifest.
RootModule = 'PSModuleTest.psm1'

This is normally the first field in the manifest if you used New-ModuleManifest to create it.

Your module won't be visible in a session until it has either been explicitly loaded or auto-loaded. It can only auto-load if commands can be discovered relatively easily (or the commands in the module are cached).

This command will show modules, included those which are not loaded:

Get-Module -ListAvailable
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks Chris. That worked but still my module is not being loaded automatically. Am I missing something here?
Automatic loaded is best assisted by explicitly listing the commands you wish to export in your module manifest (FunctionsToExport).
I did that but did not work. Had to load module from $PsHome\Profile.ps1 file. Is that the right approach?
It's an approach. There's nothing wrong with it if you always want the module available. I tend to do the same for the modules I always want to be there for me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.