I need help to find the current path of my .psm1 file with in the script. $MyInvocation.mycommand.path is working fine for .ps1 files but not for .psm1
Could someone suggest any other command which works similar to $MyInvocation.mycommand.path for .psm1 files.
Are you running PowerShell 3.0 or later? If so, just use the automatic $PSScriptRoot variable and save yourself some hassle.
If your code needs to support PowerShell 2.0, then what I would do is set a script-scope variable when the module is imported (to $MyInvocation.MyCommand.Path), and then reference that variable from inside your functions. The problem is that $MyInvocation contains different information when you access that variable from inside the function, as opposed to out in the script's scope.
$script:modulePath = $MyInvocation.MyCommand.Path
Write-Host "Module path: $script:modulePath"