Powershell Version Of Running If Called From Shell
Jul 17, 2021
The aim of this playbook🏁 is to show the PowerShell version of a Python idiom that executes a script if called from a shell, but imports of called from REPL
def main()
#CODE...
if __name__ == "__main__":
main()
1. notes
- Use
$MyInvocation.Invocation
- it has information about how the script was started.
- I use dot sourcing operator (
.
), but you if needed, use call opetator (&
) - If called with
. foo.ps1
, themain()
executes. - If imported with
Import-Module foo.ps1
, nothing happens (useful for importing with $profile, etc.)
#foo.ps1
Function main {
# CODE
}
If ($MyInvocation.InvocationName -eq '.') { main }