Using PowerShell To Sort Or Remove Duplicates From Text Files

 

Here you will find two PowerShell scripts that will allow you to sort the contents of a text file alphabetically and save the sorted file as a new text file to preserve the original or existing file. The first script merely sorts the contents of a text file. The second script sorts the contents of a text file and also removes any duplicate lines or entries that may be in the original text file.

 

In most of my VBS scripts I have references to a text file called MachineList.Txt. This is the suggested text file name to store a list of machines or IP addresses for the script(s) to use as an input file. This is easier than entering one machine name or IP address at a time into a VBS script and being Lazy as I am it is the way I write most of my scripts. As a result I wrote the PS1 script to sort the MachineList text file and later realized that I had much to my chagrin duplicates in some of the files and wrote the PS1 script to remove the duplicates. Be sure to change the "C:\DirectoryFolder\MachineList.Txt" to the directory folder name and text file name as needed.

 

Sort The Contents Of A Text File

 

$TextFile = $TextFile = "C:\DirectoryFolder\MachineList.Txt"

$NewTextFile = "C:\DirectoryFolder\NewMachineList.Txt"

GC $TextFile | Sort > $NewTextFile

 

Sort And Remove Duplicates From A Text File

 

$TextFile = $TextFile = "C:\DirectoryFolder\MachineList.Txt"

$NewTextFile = "C:\DirectoryFolder\NewMachineList.Txt"

GC $TextFile | Sort | GU > $NewTextFile

 

Additional Related Posts:

 

HTA Script To Remove Duplicates From A Text File

http://myitforum.com/cs2/blogs/dhite/archive/2007/05/27/hta-script-to-remove-duplicates-from-a-text-file.aspx

 

Using The Ultra-Edit Text Editor To Sort And Remove Duplicate Lines From Files

http://myitforum.com/cs2/blogs/dhite/archive/2006/05/21/20470.aspx

 

 

Published Sunday, July 29, 2007 2:03 PM by dhite
Filed under:

Comments

No Comments