Help with basic practice probelm

Here is my solution for the following prompt:
Using your previous code, display the username, the number of processes, the total workingset size. Set no username to NONE.

$UserNames = (Get-Process -IncludeUserName | Select-Object UserName -Unique | Sort-Object UserName).UserName
for ($i = 1; $i -le $UserNames.Length; $i++) {
    [int32[]]$NumProcesses += (Get-Process -IncludeUserName | Where {$_.UserName -eq $UserNames[$i]}).Count
    [int64[]]$TotalWorkingSet += ((get-process -IncludeUserName | Where-Object {$_.username -eq $UserNames[$i]}).WorkingSet | measure -Sum).Sum
for ($i = 0; $i -le $NumProcesses.Length - 1; $i++) {
    if ($UserNames[$i] -eq $null) {
        Write-Host 'None'
    } else {
        Write-Output $('Username: ' + $UserNames[$i] + '; Processes Running: ' + $NumProcesses[$i-1] + '; Total WorkingSet: ' + $TotalWorkingSet[$i-1])

Please show me the best possible solution for the prompt, alternatives, and advice on how I completed the task.

Welcome to the forum. :wave:t4:

If this is an internship assignment or homework question we try to avoid assisting with those as the first port of call should be your tutor or supervisor.

Thank you for the warm welcome.

I am learning PowerShell for my IT job on my own accord. After finishing this practice problem I have a strong feeling that there is a lot of ways that this task could be completed more cleanly and effectively. If this is something you could share info on I would greatly appreciate it.

Ah, I see. … then … I’d use something like this:

$UserNameList = 
    (Get-Process -IncludeUserName | 
        Select-Object UserName -Unique | 
            Sort-Object UserName).UserName
$Result =
foreach ($UserName in $UserNameList) {
    if($null -eq $UserName){Continue}
    $UserProcessList = 
        Get-Process -IncludeUserName | 
            Where-Object -Property UserName -EQ -Value $UserName 
        User       = $UserName
        Process    = $UserProcessList.Count
        WorkingSet = ($UserProcessList.WorkingSet | Measure-Object -Sum).Sum

Usually there are several ways. But what counts in the end is the result and if you’re satisfied. :wink:

Wow, that is a beautiful solution. I am glad that I asked. Thank you very much for sharing. Hopefully I can work some of the methods you used in future scripts. Custom PowerShell objects will definitely come in handy. I was wondering how to format the result in a table but could not figure out how to using Write-Object.

Write-Object? :thinking: :wink:

It depends pretty much whatfor the output should be. Most of the time it’s enough to just drop what you want to see to the default output stream.
So just for display purposses it does not matter if you do

Write-Output "Very important message ..."

… or …

"Very important message ..."

… or even …

Write-Host "Very important message ..."


This may help you developing your own (a good) style:

And here you have some info about [PSCustomObject]

1 Like

Thank you for your quick response and sharing those resources. I would not have expected to get help within minutes!

I will keep working on my chops and poking around on the forums. Expect another topic from me here soon.