Skip to content
Advertisement

Powershell sorting columns

I was able to sort the column and remove duplicates using -Unique

My problem is that I can’t remove a specific row using the status property

ID Name Status
1 John Current
1 John Future
2 Mary Future
2 Mary Notice
3 Paul Future

I want the rows to be unique by status order.

  1. Current
  2. Notice
  3. Future

Example: If there’s no Current but have Notice and Future, Notice will be selected.

ID Name Status
1 John Current
2 Mary Notice
3 Paul Future

Would you please let me know what I need to add on my script?

Advertisement

Answer

Thanks @mklement0 for the more clever approach (as always hehe):


Try with this:


Assuming the sort priority is Current > Notice > Future, I think this should work, though I’m not sure if it’s possible to do it on one go, as you can see I’m pipping Sort-Object to Sort-Object again. Would love to know if it’s possible to sort and get unique on one go.

This results in:

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement