Get-Unique not working as expected

Ask your 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.
This topic is 6 years and 2 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
mtartaglia
Posts: 101
Last visit: Mon Dec 19, 2022 11:45 am

Get-Unique not working as expected

Post by mtartaglia »

I have a bunch of files in a folder from a few years back that I want to parse through and list just the years. The catch is I only want to list each year once so I can use that list in another part of my script. What happens is it only lists one year. Here is the code:


$items = get-childitem -Path c:\logfiles\ -recurse

$years = Get-ChildItem -Path $sourcepath\$folder | Select -Property {$_.LastWriteTime.Year} |get-unique


When I look at the variable $years it just lists 2016. I have confirmed that there are files form 2017 and 2018 as well.

To just summarize what I want to list is the following from a list of hundreds of files.

2015
2016
2017
2018

any help would be appreciated as I have never used the get-unique cmdlet before.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Get-Unique not working as expected

Post by jvierra »

I think this is what you are trying to do:

Get-ChildItem | Select @{n='Year';e={$_.LastWriteTime.Year }} -Unique
User avatar
mtartaglia
Posts: 101
Last visit: Mon Dec 19, 2022 11:45 am

Re: Get-Unique not working as expected

Post by mtartaglia »

I was looking at it all wrong apparently. Thank you. that should work just fine.
This topic is 6 years and 2 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked