6

So at the company I am currently doing my internship at, they have about 20 'technical laptops' that have company-specified software. They are located in the C:\ directory.

For example:

aaa.exe is located at C:\aaa\aaa.exe

bbb.exe is located at C:\bbb\bbb.exe

What I actually need is a .ps1 script to show these specific executables in a list.

What I have so far is: Get-ChildItem C:\ -recurse | where {$_.extension -eq ".exe"}

Now, I believe it's just edit the search query because this gives me all the .exe files on the C:\ drive and I just need aaa and bbb.

FYI: I need the script so that I can use it to monitor zabbix agents and see if the specific software is on the computer so I can run this script on the Zabbix Server.

Richlv
  • 3,954
  • 1
  • 17
  • 21
Jp Morgan
  • 171
  • 1
  • 1
  • 5
  • Do you also need the path to these executables? – Vivek Kumar Singh Mar 05 '18 at 12:32
  • The path is already shown with the command that I use to filter and find only .exe files Output for example: Directory: C:\aaa Mode LastWriteTime Length Name -a---- 21/05/2012 9:54 99999 aaa.exe I just want the output to only show aaa and bbb and not everything on the C:\ drive – Jp Morgan Mar 05 '18 at 12:35
  • You can improve the filter to add specific files you are looking for or you can use `-include` parameter. The former is a long way, the latter being a short one. Something like this - `gci C:\ -recurse -include 'aaa.exe', 'bbb.exe'`. Is that what you want? – Vivek Kumar Singh Mar 05 '18 at 12:39
  • It's definitely a big helper thanks! I just noticed that these technical laptops don't have a nice structure when it comes down to having software in the same directory. How would I apply this to search not only on the C:\ drive but also on the D:\ drive? I'm sorry. I'm not too familiar with queries – Jp Morgan Mar 05 '18 at 12:45

2 Answers2

11

You are looking for the -include parameter:

Get-ChildItem C:\ -recurse -include "aaa.exe", "bbb.exe"
mklement0
  • 382,024
  • 64
  • 607
  • 775
Martin Brandl
  • 56,134
  • 13
  • 133
  • 172
1

@Martin Brandl has already answered your first question. For the second part of your question, you can do something like this -

$AllDrives = Get-PSDrive -PSProvider 'FileSystem'
foreach ($drive in $AllDrives)
{
    Get-ChildItem -path $drive.Root -recurse -include "aaa.exe", "bbb.exe"
}

Or you can do it in one line -

Get-PSDrive -PSProvider 'FileSystem' | % {Get-ChildItem -path $_.Root -recurse -include "aaa.exe", "bbb.exe"}

You can change the -PSProvider parameter if your executables are on other drives other than FileSystem drives.

Vivek Kumar Singh
  • 3,223
  • 1
  • 14
  • 27
  • Is there also a way to find the version of the executable ? – Jp Morgan Mar 05 '18 at 13:01
  • 1
    `[System.Diagnostics.FileVersionInfo]::GetVersionInfo("somefilepath").FileVersion` Check [this](https://blogs.technet.microsoft.com/askpfeplat/2014/12/07/how-to-correctly-check-file-versions-with-powershell/) and [this](https://stackoverflow.com/questions/30686/get-file-version-in-powershell) – Vivek Kumar Singh Mar 05 '18 at 13:07
  • **Thanks a lot !** – Jp Morgan Mar 05 '18 at 13:11