Need help with Powershell scripting basics

I have to write a script for my class with an output like this:

Mon 11 
Tue 2
Wed 5
Thr 7
Fri 13

Mon 11
Tue 2
Wed 5
Thr 7
Fri 13

Total 38

Average 7.6

Mon had the most intrusions at 11
Tue had the least intrusions at 2

I can get all the information but I can’t figure out how to get the day to show for the last two lines.

$days = @("Mon", "Tue", "Wed", "Thr", "Fri")
$attacks = @(0,0,0,0,0)
$total = 0
$most = 0
$least = 99999
Write-Host "--Enter--Intrusions--"
for ($day = 0; $day -lt $days.Length; $day++) {
$attacks[$day] = Read-Host $days[$day]
write-host "------INTRUSIONS------"
Write-host " "
for ($day = 0; $day -lt $days.Length; $day++) {
Write-Host $days[$day] " " $attacks[$day]
$total = $total+=$attacks[$day]
$average = $total / 5
Write-Host " "
write-Host "Total" $total
write-host " "
write-Host "Average" $Average
for ($day = 0; $day -lt $days.Length; $day++) {
if ($attacks[$day] -gt $most) {
$most = $attacks[$day]
for ($day = 0; $day -lt $days.Length; $day++) {
if ($attacks[$day] -lt $least) {
$least = $attacks[$day]
Write-Host $most
Write-Host $least

Randall, welcome to Please take a moment and read the very first post on top of the list of this forum: Read Me Before Posting! You’ll be Glad You Did!.

When you post code, error messages, sample data or console output format it as code, please.
In the “Text” view you can use the code tags “PRE”, in the “Visual” view you can use the format template “Preformatted”. You can go back edit your post and fix the formatting - you don’t have to create a new one.
Thanks in advance.

If you have related data you should save them together as related data. :wink:

I’d do it this way:

$Data = @'
'@ |

foreach ($item in $Data) {
    [INT]$item.AttackCount = Read-Host -Prompt "Enter amount of attacks for '$($item.Day)'"

$Result = $Data | Measure-Object -Property AttackCount -AllStats
$SortedData = $Data | Sort-Object -Property AttackCount

"  Total amount of attacks: '$($Result.Sum)'"
"Average amount of attacks: '$($Result.Average)'"
"Minimum amount of attacks: '$($Result.Minimum)' at '$($SortedData[0].Day)'"
"Maximum amount of attacks: '$($Result.Maximum)' at '$($SortedData[-1].Day)'"

Of course - as always - there’s a lot of room for improvements. :wink:

I keep getting an error at the -AllStats in line 16.

When you post error messages you should always post them completely.

And what have you tried to solve this issue? You should ALWAYS read the help for the cmdlets you’re about to use. Read it complete including the examples to learn how to use them.
You may compare the two help versions :

Then you may think about for a moment what version of Powershell do you use and the solution will be obvious. :wink:

Thank you for your help!

:wink: … just because I’m curious and it might help others looking for the same or a similar issue - wouldn’t you like to share your solution?

Since I was running Powershell 5.1 the -AllStats wasn’t available. Once I downloaded Powershell 7 the script worked perfectly.

Well … I would have simply changed -AllStats to -Sum -Average -Maximum -Minimum as that would provide the needed results. But it’s a great idea as well to have the new version of Powershell and maybe benefit from new features. :wink: