7

I try to write a script in PowerShell which finds files that have same names. After that I want to know how many duplicate names of group that files. I succeded to finding files like that:

foreach ($i in Get-ChildItem *.)
{
    Write-Host (Get-ChildItem $i).BaseName
}

But after that I need a command like in Linux cat file | sort | uniq -d. This -d parameter is my goal in PS.

I tried following command, but doesn't work:

$var = (Write-Host (Get-ChildItem $i).BaseName | Sort-Object -Unique).Count

# Let's say there are files '1.mp4,1.mkv,2.mp4'
# I want variable var's value is 1, because only one group that has same file names

  • Your question is not clear - "I want variable var's value is 1, because only one group that has same file names" Is the 1 numeric because there is 1 group or is is meant to be the BaseName of the file that has duplicate names? if there are 2 groups with duplicate BaseNames are you looking for 2 as the answer or 2 different BaseNames? Please can you edit the question to clarify? – DavidPostill Mar 8 at 18:48
3

This should do what you need,

$var = Get-ChildItem | Group-Object -Property BaseName | Where-Object { $_.Count -gt 1} | Select-Object -ExpandProperty Name 
| improve this answer | |
  • Thank you so much! Exactly what I needed. – Serhat Çelik Mar 8 at 9:59
2

I tried following command, but doesn't work:

$var = (Write-Host (Get-ChildItem $i).BaseName | Sort-Object -Unique).Count

# I want variable var's value is 1, because only one group that has same file names

The other answer by jfrmilner stores the BaseName in var not the count as you have requested in the question.

The following command stores the count (number of distinct sets of files sharing a command BaseName) in var, which may be a more useful general command:

 $var = (Get-ChildItem $i).BaseName | Group-Object | ?{ $_.Count -gt 1 } | Measure-Object | Select-Object -ExpandProperty Count

Example 1 - One set of files sharing a common basename (test)

> Get-ChildItem


    Directory: F:\test


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a----       12/02/2020     12:18           1272 test.txt
-a----       16/02/2020     17:51             72 test.cmd
-a----       19/11/2019     21:50             19 output.txt
-a----       08/12/2019     18:47            119 GetBits.cmd
-a----       31/12/2019     14:31       26876158 SystemEvents.xml
-a----       08/01/2020     21:30            845 notepad++ regexp answer template.txt
-a----       12/02/2020     11:00          17755 usb.csv
-a----       01/03/2020     10:05            264 index.jpg
-a----       01/03/2020     10:09            264 New.txt


> (Get-ChildItem $i).BaseName | Group-Object | ?{ $_.Count -gt 1 } | Measure-Object | Select-Object -ExpandProperty Count
1
>

Example 2 - Two sets of files sharing a common basename (test and New)

> Get-ChildItem


    Directory: F:\test


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a----       12/02/2020     12:18           1272 test.txt
-a----       16/02/2020     17:51             72 test.cmd
-a----       19/11/2019     21:50             19 output.txt
-a----       08/12/2019     18:47            119 GetBits.cmd
-a----       31/12/2019     14:31       26876158 SystemEvents.xml
-a----       08/01/2020     21:30            845 notepad++ regexp answer template.txt
-a----       12/02/2020     11:00          17755 usb.csv
-a----       01/03/2020     10:05            264 index.jpg
-a----       01/03/2020     10:09            264 New.txt
-a----       08/03/2020     15:35              0 1
-a----       08/03/2020     15:52              0 New.xxx


> (Get-ChildItem $i).BaseName | Group-Object | ?{ $_.Count -gt 1 } | Measure-Object | Select-Object -ExpandProperty Count
2
>
| improve this answer | |
  • Read the question again. "1" is the example BaseName that is shared by multiple files. So the first answer is closer, even though he lists the names of the files that share the BaseName. – Keith Miller Mar 8 at 18:37
  • @KeithMiller Ah. Question is a little unclear then ... as his example code is ending with Count ... – DavidPostill Mar 8 at 18:38
  • Never look at the OP's code, it will just confuse you! :D – Keith Miller Mar 8 at 18:41
  • @KeithMiller I've asked for clarity, so we will see ... – DavidPostill Mar 8 at 18:48
  • Most likely, the OP just needed to be made aware of the Group-Object cmdlet. He knew what he wanted, just not that familiar with PowerShell. – Keith Miller Mar 8 at 19:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.