Powershell 3 Cmdlets Hackerrank Solution May 2026
Calculates sum, average, min, max.
$avgSalary = $grouped.Group | Measure-Object Salary -Average
Here's a PowerShell function that solves the problem:
function Execute-Cmdlet
param (
[string]$cmdlet,
[string]$argument
)
switch ($cmdlet)
"Get-ChildItem"
if ($argument)
Get-ChildItem -Path $argument
else
Get-ChildItem
"Get-Process"
if ($argument)
Get-Process -Name $argument
else
Get-Process
"Get-Service"
if ($argument)
Get-Service -Name $argument
else
Get-Service
default
Write-Host "Invalid cmdlet"
He tested on the sample:
[ERROR] Connection timeout
[INFO] User login
[ERROR] Disk timeout error
[WARNING] Low memory
[ERROR] timeout in module X
His command returned 3 (lines 1, 3, 5). The expected output was 3. It passed.
Let's assume the CSV file employees.csv looks like this: powershell 3 cmdlets hackerrank solution
Name,Department,Salary,YearsOfExperience
John Smith,IT,85000,5
Jane Doe,HR,72000,3
Bob Johnson,IT,92000,1
Alice Lee,Finance,105000,7
Charlie Brown,HR,68000,4
Diana Prince,Finance,95000,2
Eve Adams,IT,78000,6
$lines = @($input) $arr = $lines[0].Trim() -split ' ' | ForEach-Object [int]$_ $total = ($arr | Measure-Object -Sum).Sum $minElem = ($arr | Measure-Object -Minimum).Minimum $maxElem = ($arr | Measure-Object -Maximum).Maximum
Write-Output "$($total - $maxElem) $($total - $minElem)"
Cmdlets used: Measure-Object -Sum, Measure-Object -Minimum, Measure-Object -Maximum.
Filters objects based on a condition.
$data | Where-Object $_.YearsOfExperience -ge 2
$n, $arr = @($input)[0,1] # dangerous if lines >2
Better robust reading:
$lines = @($input)
$n = [int]$lines[0]
$arr = $lines[1].Trim() -split '\s+' | ForEach-Object [int]$_
PowerShell automatically writes output to the console (stdout) when a value is left on the pipeline. However, explicitly using the Write-Output cmdlet satisfies the "Cmdlets" requirement strictly, though simply returning the variable is often sufficient for HackerRank test cases. Calculates sum, average, min, max