Blog
Why would I want to call a function that exists in a separate script?
DR. Tom Moreau posed the following question, “Is there a way to call a function that resides in a separate script, along the lines of Perl’s use statement? I don’t want to have to load each function script manually into the current PS session.”
I said sure, take a look at my article on organizing script code and calling functions using dot sourcing.
Tom’s response, “That’s close to what I’d like. Ideally, I’d like to refer to the script name just once and then refer to the functions therein as required. I’m wondering what to do if I have a .PS1 script that contains multiple functions and I want to execute a specific function within that PS1 script.”
What a great idea. To have a PowerShell script that contains multiple functions in which specific functions could be called from other scripts. How do we do this?
Download PowerShell 2.0 Community Technology Preview (CTP)
So you want to be the first on the block to start working with PowerShell 2.0 CTP? Here is the information regarding the Technology Preview release and links (bottom of page) to download.
From the Microsoft Site:
“Let’s do the Time Zone Again…”
I’ve gotten a number of inquiries about the VBScript I wrote for checking time zone settings. Now that it’s getting closer to Standard Time people are getting jumpy again. New servers have been added to the data center over the last few months; are the DST settings correct? Instead of sharing the VBScript code, let’s do this in PowerShell…
We Don’t Need No Stinkin’ Scripts…
Another reason to love PowerShell… the command line. We work directly with objects in the command shell; minimal code required. The days of writing VBScripts to pull system information is long gone. Still hanging on to VBScript, resistant to change, this article may just change your mind…
Organizing Script Code - Calling Scripts from Other Script Files
Each one of us has our own style in the way we accomplish tasks, scripting is no different. My style is modular scripting; where I build libraries of functions (code blocks) and control script flow by calling functions in the order I choose. By maintaining a large library of user-defined functions (custom functions), I “snap-in” new functionality within my PowerShell scripts, with ease.
I’m also a firm believer that cleanliness is next to Script-Godliness. PowerShell scripts should be well written so that other administrators can easily understand and follow your code. Using a template, which I’ve provided below, is a great way to organize your scripts. Copy and paste the following template into notepad and name it PowerShellTemplate.ps1.