Find out if a script was dot-sourced or not

All PowerShell scripts have a default variable called $myinvocation.

Here’s the variable for a script I run directly:

And here’s the variable for a script I dot-source:

So if you want to check whether a script was dot-source or not check $myinvocation.InvocationName. If dot-sourced that would be a dot.