Windows PowerShell to find duplicate lines in a file

13,712

Solution 1

You can also use the Group-Object cmdlet to see if any lines occur more than once:

e.g.

Get-Content test.txt | Group-Object | Where-Object { $_.Count -gt 1 } | Select -ExpandProperty Name

Solution 2

Used the Commands mentioned below and it worked.

PS C:\Projects> $OriginalContent, $UniqueContent = (Get-Content .\File.txt), (Get-Content .\File.txt | Sort-object -unique)
PS C:\Projects> compare-object $originalcontent $uniquecontent

or to print the text and the count use the following command

PS C:\Projects> Get-Content .\File.txt | group-object
Share:
13,712

Related videos on Youtube

vineel
Author by

vineel

Updated on June 04, 2022

Comments

  • vineel
    vineel 7 months

    I need to find the duplicate values in a text file using power shell let's say if the file content is

    Apple
    Orange
    Banana
    Orange
    Orange
    

    Desired output should be

    Orange
    Orange
    
    • vineel
      vineel over 5 years
      @Sjark, my requirement was to know whether there are any duplicates in a file. So the output can print Orange two or three times works for me. Anyways thanks for looking into it.
  • vineel
    vineel over 5 years
    Thanks Nasir!! That really helped. I am having a similar problem posted can you please check it out. stackoverflow.com/questions/43820264/…

Related