script termination from module: exit vs. break

Hi, I have a script below that simply loads a module then writes a line.

runme.ps1 contents:

Import-Module .\mymod.psm1
write-host "from runme.ps1"

mymod.psm1 contents:

exit

Running the script outputs:

from runme.ps1

This indicates that the exit in the module does not terminate the script, but exits the module load.  Why?  Isn't it supposed to exit the script?

Just for comparison, I've done the same in ruby and python.  They both exit when exit() is called and output nothing.

And I just can't find any reference for the exit command so if you can send me links...

July 24th, 2015 2:01pm

I forgot to mention, I'm using powershell version 3 on windows 7.
Free Windows Admin Tool Kit Click here and download it now
July 24th, 2015 2:04pm

Importing a module does not execute the module.  It just loads it into memory.

July 24th, 2015 4:20pm

Thanks for your info.

I think it is executing it for the following reason.  If I put in a write-host statement into the module it does write to the console.  Example...

mymod.psm1 content:

write-host "from mymod.psm1"

If I use break instead of exit, it does terminate the script at the break statement, and nothing is output, example...

mymod.psm1 content:

break

write-host "from mymod.psm1"

Why does break terminate the script, and exit not ?

Free Windows Admin Tool Kit Click here and download it now
July 28th, 2015 1:43pm

I think you need to redo your analysis.

July 28th, 2015 2:20pm

In a module Write-Verbose and Write-Host statements are executed but most other root level commands are ignored. Once the module is loaded even reloading it will not cause these to execute.  This is likely to help with diagnostics and to help with initialization code.

exit behave like break on the initial load. 

Free Windows Admin Tool Kit Click here and download it now
July 28th, 2015 2:26pm

If you do not restart PowerShell between each test you will get faulty results as Import only works on a module the first time it is called.

July 28th, 2015 2:33pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics