PowerShell Functions

Creating Windows PowerShell Functions

This section is dedicated to creating our own PowerShell functions.  I hope that you will find the outcomes as rewarding, and the learning journey as interesting, as I did.


My learning philosophy is to learn by doing.  My teaching philosophy is to provide examples, each emphasising aspects of building functions.  As a bonus each function has a real-life use.  It would make my idea if my instructions are sufficient for you to adapt my scripts to your project.


Benefits of PowerShell Functions

Creating re-usable code
Once we have a reliable script then let’s preserve the code and spare the frustration of reinventing the wheel in 6 month time.

The key reason to build a function, rather than merely save the script in .ps1 file is that we can introduce parameters and thus adapt a core script to different situations.

I am interested in taking on-board the Japanese idea of constant improvement.  Once we have a working function it cries out for us to add parameters or tweak the ‘Process’ section.

Guy Recommends:  A Free Trial of the Network Performance Monitor (NPM)Review of Orion NPM v11.5 v11.5

SolarWinds’ Network Performance Monitor will help you discover what’s happening on your network.  This utility will also guide you through troubleshooting; the dashboard will indicate whether the root cause is a broken link, faulty equipment or resource overload.

What I like best is the way NPM suggests solutions to network problems.  Its also has the ability to monitor the health of individual VMware virtual machines.  If you are interested in troubleshooting, and creating network maps, then I recommend that you try NPM now.

Download a free trial of Solarwinds’ Network Performance Monitor


Purpose: To find the machine holding the list of network shares.
Learning Point: Study the Process section.  Get-BrowseMaster »

Simple Function

Purpose: To calculate batting average.
Learning Point: Create a function incorporating maths.  Get-BatAvg »


Purpose: To check for unsigned device drivers.
Learning Point: Classic examples of ‘Switch’ parameters.  Get-Driver »


Purpose: To discover which Windows services are running.
Learning Point: See how to declare string Switches.  Get-ServiceStatus »


Purpose: To refine the built-in Ipconfig command.
Learning Point: Neat use of ElseIf logic.  How to organize header sections.  Get-IpConfig »


Purpose: To interact with Outlook.
Learning Point: Create and use -ComObject.  Send Email »

Guy Recommends:  SolarWinds’ Log & Event Management ToolSolarwinds Log and Event Management Tool

LEM will alert you to problems such as when a key application on a particular server is unavailable.  It can also detect when services have stopped, or if there is a network latency problem.  Perhaps this log and event management tool’s most interesting ability is to take corrective action, for example by restarting services, or isolating the source of a maleware attack.

Yet perhaps the killer reason why people use LEM is for its compliance capability, with a little help from you, it will ensure that your organization complies with industry standards such as CISP or FERPA.  LEM is a really smart application that can make correlations between data in different logs, then use its built-in logic to take corrective action, to restart services, or thwart potential security breaches – give LEM a whirl.

Download your FREE trial of SolarWinds Log & Event Management tool.

More Examples of PowerShell Functions


Purpose: To research WMI classes.
Learning Point: How to declare parameters.  Get-WMIClass »

Plist Function

Purpose: To list Windows processes.
Learning Point: Declare parameter at the start.  Plist (Processes) »


Purpose: To delete old files in the recycle bin.
Learning Point: Great examples of ‘If’ logic.  Clear-RecycleBin »


Purpose to clean out the Windows eventlogs.
Learning Point: Create a function to solve a real-life problem.  Clear-WinEvent »

Microsoft’s Help-Files

For further research try PowerShell’s own About-Functions … series of help files.

Get-Help about_Functions_Advanced_Methods
#Get-Help about_Functions_Advanced_Parameters
#Get-Help about_Functions_CmdletBindingAttribute
#Get-Help about_Functions_OutputTypeAttribute
#Get-Help about_Parameters

Note: Just remove the '#' to check the other 4 help files.

More Windows PowerShell Sections

If you like this page then please share it with your friends


See more Windows PowerShell sections

PowerShell Home   • Introduction  • Dreams   • 3 Key Commands   • Free Permissions Analyzer

PowerShell Real-life Examples   • PowerShell Techniques   • PowerShell Syntax   • Contact

PowerShell ISE  • PowerShell 2.0   • PowerShell v 3.0   • PowerShell v 4.0   • PowerShell v 4.0

Please email me if you have a better example script. Also please report any factual mistakes, grammatical errors or broken links, I will be happy to correct the fault.