PowerShell – How to Test a Functions Parameters

This post if from our Windows PowerShell Scripting and Tool Making online video training library.

Interface Video Training is available for a FREE 3-Day trial, no credit card necessary. The entire Video Training Library is available for only $25 per month.

One of the first things I do when I begin coding the Function, is to test and see if my parameters are actually working the way I want them to. Most of that testing will go here in the Process block.


When my function is executed, PowerShell will execute whatever’s in the Begin block first.


If I’ve piped input into my function, then the process block will execute one time for each object that I piped in.

My pipeline variable – in other words, the parameter that accepted the pipeline input – will only contain one object at a time in that scenario.


When PowerShell has run through all of the piped in objects, it will then execute the End block once.

If I don’t pipe anything in.


If I just run my command using parameters, then the Begin block will execute first, the Process block will execute one time, and my pipeline variable, or parameter, will contain all of the objects that were specified to that parameter.

Once the Process is finished executing one time, End will execute one time.

Keep in mind, if things are piped in, ComputerName will only contain one thing at a time. But if information is specified to ComputerName as a parameter – such as in this use case or this use case – ComputerName could contain one item or multiple items.


I need to make sure it’s capable of receiving multiple items, so I’ll change its declaration to include the two square brackets. [string[]]$computerName


That means it will always be an array, even though it might only contain one item. Because it’s an array, I’ll have to enumerate through it, getting one thing out of it at a time – Foreach computer in ComputerName. Within this Foreach construct, I should have a computer variable that contains only one computer name at a time, regardless of how my function was actually called.


For right now, I’ll use Write-Host to output that, so I can test it.


I’ll save the script now. Put this in the root of my C drive, so it’s easy to get to, or if you’ve got a convenient folder you can store it in. We’ll call it FunctionTest.

I cannot test this by running the script. If I run the script, it will declare the function, and then it will reach the end of the script and never actually run it.

For right now, just for testing purposes, I’ll copy and paste a few of my usage examples to the clipboard and bring those here and just paste them right into the bottom of the script.



I need to make sure I have this computers.txt file created.


I do have a computers.txt. Let’s just edit it real quick with notepad to make sure it contains valid computer names. We’ll put in Localhost and Ussdc and save the file.


Now I can start testing this. I really only want to do one at a time, so I’ll comment out my test lines, except the first one, and I’ll just run this.


Let’s talk about what I’m expecting to happen. I’m specifying a single parameter, ComputerName, and a single value for it.


I’m expecting my script to simply display that ComputerName because right now that’s all I’ve asked it to do, and it did.


I’ll comment that one out. We’ll try the next one.


This time I’m asking it to display two computer names, and it did.


Great, that’s working perfectly.

Those are examples of specifying information to the parameter. Let’s try piping in some information. This should pipe in by value, ValueFromPipeline.


There are two computer names in that file, so it should display both of those computer names. And we had a problem. It could not find the file because it does not exist.


When you’re working here in the ISE, it’s actually focused on a particular folder. Here it’s in the root of my profile. My computers.txt file is in the root of the drive. It’s easier to specify a complete path.



Try this again.

And it displayed both of the names.


We’ll undo the (c:\).

Now I’m testing to make sure that my Alias –Host also works.


And that worked as well. It displayed that name, perfect.


We can do one more test. Remember, I have one more use case over here.


This is just slightly more difficult to test. Go ahead and comment that one out. We’ll paste this one in.


Because this is using the ADComputer cmdlet, I need to make sure I’m first importing the Active Directory module that contains that cmdlet. Get-ADComputer –filter * | Select-Object @{n=’ComputerName’ ;e={$_.Name}} | Get-DJSInfo


What I’m doing here is retrieving the computers, changing their name property to a ComputerName property, and piping it to get DJOSInfo.


I don’t know how many computers I have in Active Directory, but I’m expecting this to output all of their names right now. We’ll save this and run it.


Great, that worked perfectly. It looks like it retrieved everything. It was able to successfully turn the name property into a ComputerName property. That attached to my ComputerName parameter by property name. That put them into the ComputerName variable, and I was able to display them.


It looks like so far, I’m doing well. My initial test looks good. I can comment these out, but I’ll leave them in the script because I’ll need them again when I start doing my main coding.

Next we’ll look at how to write the Main Code for a Function

Posted in PowerShell | Posted in , , , , , , , , , , , | Leave a comment

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">