Environment
I have the following folder structure where I store powershell modules:
C: PsModules ... util util.psm1 (this contains implementation of 'Test-Function') util.test.ps1 ftp ftp.psm1 http.test.ps1 ...
c:\PsModules has about 50 folders and modules.
I set the PSModulePath environment PSModulePath to include c:\PsModules . This is similar to the conditions for “well-formed modules” described in the Microsoft documentation and this answer .
Symptoms
Sometimes Test-Function does not appear automatically when it is called from ISE. In fact, with any new start of ISE, there are always some (seemingly unpredictable) modules that are not found automatically. For example, Test-Function 's automatic search failure looks like this:
PS C:\> Test-Function Test-Function : The term 'Test-Function' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. ... + FullyQualifiedErrorId : CommandNotFoundException
At first glance, this seems to indicate that util.psm1 not "well formed." If it was not “correctly formed,” then ListAvailable should not work. But it works:
c:\> get-module -ListAvailable util Directory: c:\PsModules\util ModuleType Version Name ExportedCommands ---------- ------- ---- ---------------- Script 0.0 util PS C:\> Test-Function ... + FullyQualifiedErrorId : CommandNotFoundException
In addition, after calling Get-Command for a module, the commands in the module are available for general use:
c:\> Get-Command -Module util CommandType Name ModuleName ----------- ---- ---------- Function Test-Function util c:\> Test-Function Call to Test-Function succeeded!
Questions
Is automatic detection and automatic module loading reliable and predictable?
How to troubleshoot why powershell sometimes cannot find a command before calling Get-Command -module ?
Is it a bad practice to rely on powershell to automatically load modules? If so, what is good practice for automatically loading modules?