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.
- A Simple PowerShell Function
- Function to List .Lnk Shortcuts
- Parameters for Functions
- Plist (Processes)
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.
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.
Purpose: To find the machine holding the list of network shares.
Learning Point: Study the Process section. Get-BrowseMaster »
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 »
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.
More Examples of PowerShell Functions
Purpose: To research WMI classes.
Learning Point: How to declare parameters. Get-WMIClass »
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 »
For further research try PowerShell’s own About-Functions … series of help files.
Note: Just remove the '#' to check the other 4 help files.
More Windows PowerShell Sections
- Section Dedicated to Real-life Tasks
- Section Dedicated to PowerShell Techniques
- Section Dedicated to Syntax
- PowerShell Introduction
- PowerShell Tips
If you like this page then please share it with your friends
See more Windows PowerShell sections
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.