Select-Object -last 1?

Ask your Windows PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
Post Reply
User avatar
MarvelManiac
Posts: 43
Joined: Mon May 01, 2017 8:28 pm

Select-Object -last 1?

Post by MarvelManiac » Mon Feb 12, 2018 3:10 pm

Hello,

So I gather applications off of a PC
Our log files tell us what applications have been installed.

I could pull the registry but I've found looking for the .log files to be much quicker

I have a problem where we have mutliple versions of applications
Chrome
Acrobat
Visio
etc..

If my list looks like this
Adobe Acrobat Pro DC 15
Adobe Acrobat Pro DC 17
Google Chrome 50.0.2661.94
Google Chrome 59.0
Microsoft Visio Pro for Office 365 1512 15.0.4779.1002
Microsoft Visio Pro for Office 365 1609 16.0.7369.2127

How would I get it to look like this
Adobe Acrobat Pro DC 17
Google Chrome 59.0
Microsoft Visio Pro for Office 365 1609 16.0.7369.2127

I only want to display the newest if there are more than one that eqaul

User avatar
jvierra
Posts: 11843
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Select-Object -last 1?

Post by jvierra » Tue Feb 13, 2018 2:54 am

There are no duplicates so how will you know which ones to compare?

User avatar
skalizar
Posts: 17
Joined: Thu Oct 16, 2014 11:21 am

Re: Select-Object -last 1?

Post by skalizar » Tue Feb 13, 2018 6:46 am

This should work in most cases, it assumes the application name is the start of the string and contains no digits and the version is at the end of the string and contains only digits and periods. Create an object with the original name, the application name and the version, using this regex.

https://regex101.com/r/7w6Jhy/1

Your object will look something like this.

Code: Select all

App                                                      AppName                          Version
Adobe Acrobat Pro DC 15                                  Adobe Acrobat Pro DC             15
...
Microsoft Visio Pro for Office 365 1609 16.0.7369.2127   Microsoft Visio Pro for Office   16.0.7369.2127
Then you can group on AppName and select the latest version. There maybe some versions that don't sort properly, like going from 9.1.0 to 10.1.1, but this is about as close as you will get unless you can pull a numeric version number from your source.

User avatar
MarvelManiac
Posts: 43
Joined: Mon May 01, 2017 8:28 pm

Re: Select-Object -last 1?

Post by MarvelManiac » Tue Feb 13, 2018 10:45 am

I’ll post my psf later. Maybe it’ll help show

I should have done it in the first place

User avatar
MarvelManiac
Posts: 43
Joined: Mon May 01, 2017 8:28 pm

Re: Select-Object -last 1?

Post by MarvelManiac » Wed Feb 14, 2018 8:17 am

Sorry for the delay. Here is my working example with lots of corporate applications removed from the excluded applications.txt but I think you'll get the jist

You'll only need to edit lines 206, 207 to get this to work
TestMatch.zip
(104.43 KiB) Downloaded 4 times

Post Reply