One feature of PowerShell that I had a hard time figuring out was how to include one script in another. I have a number of build scripts which build the installers for our products, and there are a lot of steps that are the same between them. Up until recently I was using the proven method of copy/paste to share the functionality, but I finally decided to sit down and take the time to build out my build system properly.
The first thing I needed to do was to put similar functionality into a library and load it into each product’s build script. This library consists of a bunch of functions such as incrementing version numbers, building help files, compiling source code, and digitally signing files. The problem is how to get the functions to be available to other scripts. As you may have noticed, when you run a script none of the functions are available to you after the script ends. Here’s me running a script that has a single function called Hello:
In order to make the function in the script last after the script ends, it’s necessary to run the script preceded by a period:
With that, all symbols in the script persist and you can create any library scripts you may need.
Once you get to the point where you have enough scripts to make modularity necessary, you are to the point you should consider using some source control.
Have any other PowerShell tips that may not be obvious to everyone? Post them in the comments and share with the world (or the tiny portion of it that reads this blog, anyway.)