How to detect whether a script is dot-sourced, or loaded as part of a module using powershell v2+?
问题 Given a ps1 file as part of a module with the following code: function Get-Greeting { 'Hello {0}' -f $Env:Username } Export-ModuleMember -Function:Get-Greeting When loaded as part of the module, everything is fine. If I dot-source the script, I get Export-ModuleMember : The Export-ModuleMember cmdlet can only be called from inside a module. I know I could just add a -ErrorAction:Ignore on the Export-ModuleMember, but that's not the point. I'd like to have a script run differently whether it