You currently have a script that performs a useful task.
As you create more and more scripts, you may feel that you are creating lots of files that each have
only a single use.
Windows PowerShell has a way to combine several tools into a single file.
This is known as a script module.
However, to combine several tools into one file, you must have a way to create a separation or wrapper
around each tool.
Doing this lets Windows PowerShell know which commands to run as a set.
Windows PowerShell uses functions to provide a wrapper around a tool.
Basically, you make a function by wrapping all the contents of your script in a function declaration.
A declaration consists of the keyword function followed by a function name.
Function name should be consistent with the Windows PowerShell verb now naming convention.
For example, if you start with this.
Come let binding param string dollar computer name.
INT dollar event id get event log log name security computer name dollar computer name wear event ID
IQ dollar event ID Select first 50.
The function might resemble this function.
Get security events.
Come let binding param.
String dollar computer name int dollar event id get event log log name security computer name.
Dollar computer name where event id iq dollar event.
ID select first 50.
Notice that the contents of the function are indented, helping make it visually obvious what commands
belong to that function.
If your script includes comment based help, include that help is the first thing inside the function.
There are other areas where Windows PowerShell allows for comment based help to be added, but adding
the help immediately inside the beginning of the function is the easiest and has the fewest restrictions.
Testing the function.
Unfortunately, testing the function is now somewhat more difficult.
You cannot just run the script.
Running the script defines the function to Windows PowerShell, but as soon as the script finishes,
the function definition is deleted from memory.
The function never actually runs.
This behavior is because of a Windows PowerShell feature known as Scope.