PowerShell Get-File Function

Create a Function to List .Exe and Other ExtensionsPowerShell Get-File Function

The default action of this PowerShell function is to search for executable files in the Program Files folders.  However, I created Get-File with parameters to extend the location of the search, and to refine the type of file listed by the underlying cmdlet, Get-ChildItem.

Guy's Get-File Function


Planning the Get-File PowerShell Function

I have expanded the original concept of just finding program files, to finding any type of file providing you use the -Name and -Extension parameters.  Your idea of where the program files are stored may differ from mine, to cater for this need I have include -PathAdd.

1) Header Section – Help Comments and Function Description
This section describes what the function can do.  Another way of reading the .SYNOPSIS is to type:
Get-Help Get-File.

2) PowerShell Function Parameters
I have added four parameters called Name, Extension, PathAdd and NewLocation, as a result you can adjust the places and filenames that the function returns in its listing.

3) The Process Keyword
Scripting with Windows PowerShell is rewarding; the process section contains the engine of the cmdlet's function.  In my example below the Process section holds Microsoft's Get-ChildItem instructions, which are the base of this function.

It is here that you will also find the logic defined by a series of If … ElseIf statements.

How To Create the Get-File Function

This default action when you type 'Get-File' is to list .exe files in the built-in Program File folders.  However, you can modify the scope of its actions with a combination of parameters.

Function Global:Get-File {
This PowerShell function will get files with the exe extension.
This function returns a list of all the executable files in Program Files.
You can add other locations by using -PathAdd [Path to your files]
Filter the output of get-file.
.PARAMETER Extension
Get the file extension of your choice.
Type your own path to add to the list of places to search.
.PARAMETER NewLocation
Type the new path, the function will ONLY Use this location.
Get-File -Name w*
Get-File -PathAdd D:
Get-File -Extension dll -NewLocation H:
Param (

[parameter(mandatory=$false, Position=0)][String]$Name ="*",
[parameter(mandatory=$false)][String]$Extension ="exe",
Process {
"Listing $Name.$Extension files …"
$Prog = "C:\Program Files\"
$Prog86 = "C:\Program Files (x86)\"
$FileFull = $Name +"."+ $Extension
Set-Location $Prog
$Exe1 = Get-ChildItem $FileFull -Recurse
Set-Location $Prog86
$Exe2 = Get-ChildItem $FileFull -Recurse
# End of the core search for files.
# Start of PathAdd and NewLocation logic.
If($PathAdd -ne "") {
"Searching for more $Extension files …"
Set-Location $PathAdd
$Exe3 = Get-ChildItem $FileFull -Recurse -Force -EA SilentlyContinue
ElseIf($NewLocation -ne "") {
Set-Location $NewLocation
"Searching $NewLocation for more $FileFull files …"
$Exe3 = Get-ChildItem $FileFull -Recurse -Force -EA SilentlyContinue
$Exe1 =$Exe2 = $Exe3
#End of file collection
$Exe = $Exe1 + $Exe2 + $Exe3
$Exe | Sort-Object Name -Unique | `
Format-Table Name, @{Label = "Path" ; Expression = {$_.FullName} } -Auto
"The number of $FileFull files is approximately: " + ($Exe.Count -4)
    } # End of Process Section

Suggestion:  Here are three ideas for using my function to get files:

Get-File -PathAdd D:
Get-File -NewLocation E: -Name S*

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 My Get-File Function

Note 1: To cater for errors when searching for protected files I have appended this command to the additional locations:
-Force -ErrorAction SilentlyContinue

Note 2: As the -Name is defined as Position =0, you can just type the filename you are seeking, for example:  Driv*  (-Name Drive*) is not essential.

Note 3: Here you can use PowerShell to get the file extension thanks to the -Extension parameter.  You can even use wildcards, for example doc*.  I was pleasantly surprised that string parameters allow such wildcards without any special changes to their definition.

Note 3: You can tweak the output of Microsoft's Get-ChildItem's own parameters. 

Note 4:  You can also add -CMatch to search only for uppercase .EXE files thus:
$Exe1 = Get-ChildItem *.exe -Recurse | Where-Object {$_.Extension -cmatch "EXE"}

Note 5: The Exe.Count does not accurately return the number of files; nevertheless, it's worthwhile to show an approximation.

Troubleshooting My Get-File Function

Here is a simpler version of the main function above, I have called it Get-FileBasic.  I say simpler because there is no 'If' logic, and no parameters.

Function Global:Get-FileBasic
Process {
"Listing exe files …"
$Prog = "C:\Program Files\"
$Prog86 = "C:\Program Files (x86)\"
Set-Location $Prog
$Exe1 = Get-ChildItem *.exe -Recurse
Set-Location $Prog86
$Exe2 = Get-ChildItem *.exe -Recurse
$Exe = $Exe1 + $Exe2
$Exe | Sort-Object Name -Unique | `
Format-Table Name, FullName -Auto
"The number of .exe files is: " + $Exe.Count
    } # End of Process


Engineer's Toolset v10Guy Recommends: SolarWinds Engineer’s Toolset v10

This Engineer’s Toolset v10 provides a comprehensive console of 50 utilities for troubleshooting computer problems.  Guy says it helps me monitor what’s occurring on the network, and each tool teaches me more about how the underlying system operates.

There are so many good gadgets; it’s like having free rein of a sweetshop.  Thankfully the utilities are displayed logically: monitoring, network discovery, diagnostic, and Cisco tools.  Try the SolarWinds Engineer’s Toolset now!

Download your fully functional trial copy of the Engineer’s Toolset v10

If and ElseIf Logic in My Get-File Function

As you can see in this function example, you can combine 'If' logic with PowerShell's parameters to create some neat outcomes.

My original plan was to search known locations for .exe files.  The idea of the -PathAdd parameter is to append other locations to the search path.  Alternatively, you could use the -NewLocation parameter, in which case Get-File will only look in the path specified by -NewLocation.

Next, I decided to expand the ability of the function by introducing the -Extension parameter so that you could list files other than executables.  Finally, I added -Name so that you could filter the list to include just say setup* files.

Really, this function is just the starting point, you could bolt-on more ElseIf statements to cater for other scenarios.

If($PathAdd -ne "") {
Set-Location $PathAdd
$Exe3 = Get-ChildItem *.exe -Recurse
ElseIf($NewLocation -ne "") {
Set-Location $NewLocation
$Exe3 = Get-ChildItem *.exe -Recurse
$Exe1 =$Exe2 = $Exe3

The path in this simpler version uses *.exe, rather than the more versatile, but more complex $FileFull.

See more PowerShell parameters ยป

Summary of PowerShell Get-File Function

The main purpose of this PowerShell function is to search for program files (.exe).  However, it has 4 parameters so that you can extend the scope, and type of file listed.

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.