Pipe any array to Sort-Object (alias: sort) to sort it:
$a = 'k', 'a', 'z'
$a | sort
Output:
a
k
z
You can also sort inline without a variable:
'k', 'a', 'z' | sort
Numbers
PowerShell infers the correct numeric sort automatically:
$n = 10, 3, 25, 1
$n | sort
1
3
10
25
Without Sort-Object, a lexicographic sort would put 10 before 3. Piping numbers through sort avoids that.
Descending Order
Pass -Descending to reverse the order:
'k', 'a', 'z' | sort -Descending
z
k
a
Sort and Store the Result
Sort-Object doesn’t modify the original array — it outputs a new sorted sequence. Capture it if you need it later:
$sorted = $a | sort
Sort Object Properties
When your array contains objects, pass the property name to sort on:
Get-Process | Sort-Object CPU -Descending | Select-Object -First 10 Name, CPU
Multiple sort keys are listed in priority order:
Get-ChildItem | Sort-Object Extension, Name
Case-Sensitive String Sort
String sorting is case-insensitive by default. Add -CaseSensitive when case matters:
'b', 'A', 'a', 'B' | Sort-Object -CaseSensitive
A
B
a
b
Unique Values
-Unique removes duplicates after sorting:
3, 1, 2, 1, 3 | Sort-Object -Unique
1
2
3