PowerShell powershellsortwindows 21/02/2016

Sort text numerically with PowerShell

Examples:

Full version:

Get-Content input.txt | For-Each { [Int] $_ } | Sort-Object > output.txt

Short version with aliases:

cat input.txt | % { [Int] $_ } | sort > output.txt

Sample input file (input.txt):

10
1
1000
100
9
900
9000

Sample output file (output.txt):

1
9
10
100
900
1000
9000

Explanation

The command above sorts the contents of the input file as numbers.

First, Get-Content command reads the contents of the text file.

Content is passed to For-Each command, which casts each line to integer, thus converting the text to number.

Then, the list of numbers is passed to Sort-Object command, which sorts numbers in ascending order.

Finally, result is saved to output text file.

If the text file contains unwanted duplicate values, those can be removed from result by adding the Get-Unique as the last command in the pipeline:

Get-Content input.txt | For-Each { [Int] $_ } | Sort-Object | Get-Unique > output.txt