1

I have a PowerShell script that has gotten very big. To decrease load times, I would like to load only sections of the script that are currently being called.

Does dot sourcing a script still load it at runtime? Or does it wait until it hits that function before loading the other script?

CidV
  • 19
  • 3

1 Answers1

2

It's hard to tell what the makeup of your current script is, but I would recommend just splitting your code into functions, possibly making a module out of it if appropriate.. When that script is initially parsed, it's only defining the functions and not executing them, so it will be very fast.

Dot sourcing takes the script you've specified and immediately executes it as though it had been at that spot in your original script.

briantist
  • 45,546
  • 6
  • 82
  • 127
  • The script is loading a lot of functions (almost 4000 lines), each function loads a different GUI window. The first one being the main menu. I want to only load the functions that are selected from the main menu. – CidV Sep 19 '16 at 18:22
  • @CidV dot sourcing might help, as long as the dot line gets executed only when the menu choice is hit. You can also define functions inside of other functions (local to the parent), so that might be another way. – briantist Sep 19 '16 at 18:25
  • Upon further testing, it seems that it is not the amount of code at all. It is loading the GUI that is the issue now for some reason. Did not take long to load in the past. XAML and WPF. – CidV Sep 19 '16 at 19:56
  • Upon even further testing, it seems to be loading WPF under another user account (Users need to login as their admin account to use this tool) takes a very long time. – CidV Sep 19 '16 at 20:39
  • @CidV that makes a lot more sense. Even with 4,000 functions it should load more or less instantly. – briantist Sep 19 '16 at 20:47