PowerShell Function Get-Driver

Create a Function to See if Your Drivers Are SignedPowerShell Function Get-Driver

I created this PowerShell function to discover which of the drivers installed on my Windows 8 machine had been signed.  At the heart of the function is a built-in Windows command called DriverQuery.

Create Get-Driver: Windows PowerShell Function

 ♣

Planning the Get-Driver Function

It's not easy for a beginner to create a PowerShell Function.  There are at least 3 sections, and 4 syntax rules to master.  The purpose of this function is to list which drivers have been digitally signed (Signed = TRUE).

1) Header Section – Help Comments and Function Description
I leave this section until I am comfortable with the function, only then do I add help notes, for example, .SYNOPSIS in <# ….Adding comments … #>.

2) PowerShell Function Parameters
Param(
List of parameters
My advice is to return to the parameter's section once you have the basic Process {DriverQuery instructions} working smoothly.

3) 'Process' is a Keyword in a Function
Scripting with Windows PowerShell is rewarding, the Process section contains the engine of your script.  In my example below this section holds the logic defined by a series of If … ElseIf statements.

4) Begin and End
The keywords 'Begin' and 'End' are optional, I like to use Clear-Host in the Begin section, and I have displayed the time in the 'End' section of the script.

How To Create a Function: PowerShell Command Example

Function Global:Get-Driver {
<#
.SYNOPSIS
PowerShell function to enhance the built-in DriverQuery command
.DESCRIPTION
Includes parameters to filter for signed and unsigned drivers.
.EXAMPLE
Get-Driver -Signed
#>
[cmdletbinding()]
Param (

[Switch]$Unsigned,
[Switch]$Signed,
[Switch]$All
     )
Begin {
Clear-Host
"Retrieving driver signing information …"
          } # End of Begin section
Process {
If ($Signed){
$DrvSig = DriverQuery -Si | Select-String "True"
$DrvSig
"`n " + $DrvSig.count + " signed drivers, note TRUE column"
       } # End of If
ElseIf ($UnSigned) {
$DrvU = DriverQuery -Si | Select-String "False"
$DrvU
"`n " + $DrvU.count + " unsigned drivers, note FALSE column"
        } # End of first ElseIf
ElseIf ($All) {
DriverQuery -Si
       } # End of second ElseIf
Else {
DriverQuery
       } # End of the final Else
    } # End of Process section
End {
"`n " + (Get-Date).DateTime
      } # End of 'End' section
} # End of Get-Driver function

Three commands to try using my example function:

  • Get-Driver
  • Get-Driver -Signed 
  • Get-Help Get-Driver -Full

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

Learning Points for Creating a PowerShell Function

  • From scripting point of view, [Switch]$Signed enables you to oscillate from executing the base DriverQuery command, with executing modified code. Observe how appending -Signed results in : Select-String "True"
  • From a user's perspective, think how typing the -switch will retrieve just the information you want, for example: Get-Driver -Unsigned
  • In addition to providing the function's structure or wrapper, PowerShell supplies Select-String, which enhances the Windows QueryDriver command.
  • As an aside, I also wanted to emphasise how PowerShell can run native Windows Scripting commands such as DriverQuery.

Problems of Creating a PowerShell Function

Firstly, there is the psychological problem that you can use Windows PowerShell commands perfectly well without bothering to create functions. 

Secondly, there can be confusion for a beginner because there are so many terms vying for your attention, for example: param, process and synopsis.

Thirdly the syntax is fiddly; beginners get in a twist with the different types of bracket needed to create a PowerShell function.

There is good news, creating a PowerShell is satisfying.  When you are starting out ignore factors such writing <# comments #>, and just concentrate on the action of the 'Process' section.

My technique was to learn by doing, building on success; gradually I began to embrace terms such as Global, [Switch] and to understand the significance of the brackets and the punctuation.

See more on PowerShell function parameters ยป

Summary of Creating Get-Driver Using DriverQuery

My aim is to provide useful PowerShell function examples complete with notes, by goal is that you can modify my code for your situation.

I created this cmdlet-style function out of a desire to discover if the drivers installed on my Windows 8 machine had been signed.  At the heart of the function is a built-in Windows command called DriverQuery.

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

 


See More PowerShell Function Parameters

Scripting PowerShell Function   • PowerShell Function Clear-Recyclebin   • PowerShell Get-File

Create PowerShell Function Get-ServiceStatus  • PowerShell Function Get-Driver  • PowerShell Outlook

Show-BalloonTip   • PowerShell Function Get-IPConfig   • Free Permissions Analyzer

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.