Sorting hashtable

Ask your PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 3 years and 1 month old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
paddy75
Posts: 27
Last visit: Fri Mar 15, 2024 9:22 am

Sorting hashtable

Post by paddy75 »

Hello,
in my test environment i searching for all send and received mails on an exchange server to get the total size and total items on a daily basis. All is working fine. But when i display the results, they are in a wrong order. How can i change the order so that the oldest one is the first entry?

Code: Select all

	$daycounter = 1
	$SendReceivedMailsDaysOutput = Do
	{
		$dayendcounter = $daycounter - 1
		$daystart = (Get-Date -Hour 00 -Minute 00 -Second 00).AddDays(-$daycounter)
		$dayend = (Get-Date -Hour 00 -Minute 00 -Second 00).AddDays(-$dayendcounter)
		
		$DayReceivedMails = $ReceivedMails | Where-Object {$_.timestamp -ge $daystart -and $_.timestamp -le $dayend}
		$DaySendMails = $sendmails | Where-Object {$_.timestamp -ge $daystart -and $_.timestamp -le $dayend}
		
		$daytotalsendmail = $daysendmails | measure-object Totalbytes -sum
		$daytotalreceivedmail = $dayreceivedmails | measure-object Totalbytes -sum
		
		$daytotalsendvol = $daytotalsendmail.sum
		$daytotalreceivedvol = $daytotalreceivedmail.sum
		
		$daytotalsendcount = $daytotalsendmail.count
		$daytotalreceivedcount = $daytotalreceivedmail.count
		
		$day = $daystart | get-date -Format "dd.MM.yy"
		
		$daycounter++
		
		$SendReceivedMailsDays = [ordered]@{
			"Day" = $day
			"Total send" = $daytotalsendcount
			"Total send size ($($cmbSizeMailStat.Text))" = Convert-Size -Value $daytotalsendvol -From Bytes -To $($cmbSizeMailStat.Text) -Precision 2
			"Total received" = $daytotalreceivedcount
			"Total received size ($($cmbSizeMailStat.Text))" = Convert-Size -Value $daytotalreceivedvol -From Bytes -To $($cmbSizeMailStat.Text) -Precision 2
		}
		New-Object PsObject -Property $SendReceivedMailsDays
		
	}
	While ($daycounter -le $($nIntervallMailStat.Text))
Here a short view from the output i get:

Code: Select all

@{Day=21.01.21; Total send=0; Total send size (GB)=0; Total received=0; Total received size (GB)=0} @{Day=20.01.21; Total send=0; Total send size (GB)=0; Total received=0; Total received size (GB)=0} @{Day=19.01.21; Total send=0; Total send size (GB)=0; Tota
l received=0; Total received size (GB)=0} @{Day=18.01.21; Total send=0; Total send size (GB)=0; Total received=0; Total received size (GB)=0} @{Day=17.01.21; Total send=0; Total send size (GB)=0; Total received=0; Total received size (GB)=0}
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Sorting hashtable

Post by jvierra »

See the following article for instructions on how to sort.

https://docs.microsoft.com/en-us/powers ... rshell-5.0
paddy75
Posts: 27
Last visit: Fri Mar 15, 2024 9:22 am

Re: Sorting hashtable

Post by paddy75 »

when i put this: $SendReceivedMailsDaysOutput | Sort-Object -Property @{Expression = "Day"; Descending = $true}
it still has no effect.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Sorting hashtable

Post by jvierra »

$SendReceivedMailsDaysOutput | Sort-Object -Property Day -Descending

I strongly recommend that you learn PowerShell before proceeding. Guessing will not work and learning PowerShell will also teach you how to read the documentation and help.

Start with this: Windows PowerShell™ TFM
paddy75
Posts: 27
Last visit: Fri Mar 15, 2024 9:22 am

Re: Sorting hashtable

Post by paddy75 »

sorry when I'm to stupid. I'm still learning and a forum is for beginners and advanced guys. And by the way, this answer was not very friendly. :oops:
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Sorting hashtable

Post by jvierra »

paddy75 wrote: Fri Jan 22, 2021 4:04 am sorry when I'm to stupid. I'm still learning and a forum is for beginners and advanced guys. And by the way, this answer was not very friendly. :oops:
Why would you say that? Your question and the guess at how to use a CmdLet show you have not taken time to learn PowerShell. I wanted you to understand that you cannot learn any advanced technology by guessing. Also learning how to use PowerShell would have given you the answer and helped you to correctly understand the help docs. You tried to use a CmdLet in a way that tells me that you were trying to copy something you saw somewhere and didn't understand. The remedy is to use teh book or other learning resources to learn the basics. This is how all of technicians learn to use new technology. It is the fastest way and helps us to avoid making simple mistakes.

The link I posted is for a free book that, in my opinion, is one of teh best for anyone without programming training to use to learn programming through PowerShell. It is also written by well experienced admins with deep experience with Windows and PowerShell. It will also show you how and why PowerShell was developed specifically for admins and to assist them in their work. Even though I have decades of programming experience and have used PowerShell since before it was beta I have read that book and others as a quick way to review and improve my skills. It is just how we engineers have been trained.

Good luck.
This topic is 3 years and 1 month old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked