Menu

Virtual Geek

Tales from real IT system administrators world and non-production environment

The PowerShell Sorting Trap: Why 10 Always Comes Before 2 in Your CSV Data

When sorting CSV data with PowerShell, I observed unexpected results, as shown in the screenshot below. The initial command, Import-Csv C:\Temp\Numberings.csv, shows that the numbers in the CSV are out of sequence. To sort these numbers, I used the simple pipeline command Sort-Object specifying the Numbers property (column header).

The output clearly shows the unexpected result: 11 and 18 are incorrectly placed before 2. This poor sorting is the tell-tale sign that PowerShell is treating the column property as a string rather than a true number.

PowerShell terminal showing the failure of string sorting on numbers imported from a CSV file, followed by correctly sorted numeric data.

This unexpected result occurs because the Numbers property is currently being read as a string (str) rather than a true number. To ensure proper numeric sorting, we must explicitly convert (cast) the data type of the Numbers property to an integer (int). Here is the command you need to execute to get correct output:

Import-Csv C:\Temp\numberings.csv | Sort-Object { [int]$_.Numbers }

Screenshot demonstrating incorrect sorting in PowerShell when using Import-Csv. The top command shows default string sorting where 11 appears before 2. The bottom portion illustrates the correct numeric order achieved by casting the column data before or during the Sort-Object operation

Below is the complete command you can use with parameter and syntax. Sort-Object uses an expression in a script block to coerce the value of the Numbers property to convert to Integer.

Import-Csv -Path $csvPath | Sort-Object -Property @{
        Expression = { [int]$_.Numbers } # The Fix: Cast the string property value to [int]
        Ascending = $true
   }

Useful Articles
Powershell Active Directory: List complete hierarchy of upstream nested groups recursively of User
Powershell Active Directory: Show treeview of User or Group memberof hierarchy
Powershell Active Directory: Show treeview of nested Group members downstream hierarchy
Oneliner Microsoft Powershell Script Get members from a list of group from Active Directory in excel
Powershell Active Directory 1: Check, enable and disable child OU protect object from accidental deletion
Oneliner Powershell how to add or remove AD user members and objects in groups
Microsoft Active directory additional features - AD Recycle Bin Powershell
Powershell: Temporary group membership on Windows 2016 Active Directory PAM (Privileged Access Management Feature)
Adding user to administrators from another cross domain - Part 1
PowerShell: Copy group membership from one user to another user in Active Directory
PowerShell GUI: Copy group membership from one user to another user in Active Directory
PowerShell Active Directory: Sync group membership from one user to another user and move to OU

Go Back

Comment

Blog Search

Page Views

13509487

Follow me on Blogarama