I have a simple module called MyModule.psm1 defined as
function Show-Text($p)
{
Write-Host $p
}
Export-ModuleMember Show-Text
Module is saved here – (All works fine, the normal way via ISE or direct Powershell command line)
C:\Windows\SysWOW64\WindowsPowerShell\v1.0\Modules\MyModule (because 64-bit OS)
Now, I have a script called Test.ps1 like this
Show-Text "Hello World"
NB: I do not want to do Import-Module MyModule in the ps1 file
I am running following command from a cmd.exe window
C:\>Powershell -Command "& {Import-Module MyModule}" -File "C:\temp\Test.ps1"
I get the following error
Import-Module : The specified module ‘MyModule’ was not loaded because
no valid module file was found in any module directory. At line:1
char:17
+ & {Import-Module <<<< MyModule} -File Show.ps1
+ CategoryInfo : ResourceUnavailable: (MyModule:String) [Import-M odule], FileNotFoundException
+ FullyQualifiedErrorId : Modules_ModuleNotFound,Microsoft.PowerShell.Comm
ands.ImportModuleCommand
Can somebody help me how to execute exactly what I have stated above via cmd.exe ONLY without hard-coding or specifying the full Module Path please ?
Something on the similar lines we do for loading SystemModules – powershell.exe -ImportSystemModules
Are you running a 32-bit cmd.exe? If so then the Import-Module should work. If you are running a 64-bit cmd.exe, that will launch a 64-bit PowerShell.exe unless you fully qualify the path to the 32-bit version of PowerShell.exe (c:\windows\syswow64\windowspowershell\v1.0\powershell.exe).