Using formatting to export data views
One of the great things about PowerShell is that it gives you access to lots of information. However, this plethora of information can be a downside of PowerShell if it is not the exact information or type of information you are looking for. In previous chapters, we saw how to filter and format data; in this chapter, we will review different methods to format data to provide the information in a way that is usable to us.
How to do it...
Carry out the following steps:
Using
Get-Process
to list all running Chrome processesGet-Process chrome
To list all available attributes for our processes, execute the following code:
Get-Process chrome | Select-Object *
To return a select list of attributes, update the following command:
Get-Process chrome | ` Select-Object Name, Handles, Threads, ` NonpagedSystemMemorySize, PagedMemorySize, ` VirtualMemorySize, WorkingSet, ` PrivilegedProcessorTime, UserProcessorTime, ` TotalProcessorTime
Note
Note the use of the backtick character at the end of all but the last line. This tells PowerShell to include the contents of the lines as a single line. This allows us to more easily format the script for readability.
Combine the values of different attributes to provide more usable information
Get-Process chrome | ` Select-Object Name, Handles, Threads, ` NonpagedSystemMemorySize, PagedMemorySize, ` VirtualMemorySize, WorkingSet, ` PrivilegedProcessorTime, UserProcessorTime, ` TotalProcessorTime, ` @{Name="Total Memory";Expression=` {$_.NonpagedSystemMemorySize + ` $_.PagedMemorySize + $_.VirtualMemorySize + ` $_.WorkingSet}}
Use formatting to return the values in a human readable format.
Get-Process chrome | ` Select-Object Name, Handles, Threads, ` NonpagedSystemMemorySize, PagedMemorySize, ` VirtualMemorySize, WorkingSet, ` PrivilegedProcessorTime, UserProcessorTime, ` TotalProcessorTime, ` @{Name="Total Memory (M)";Expression=` {"{0:N2}" -f (($_.NonpagedSystemMemorySize + ` $_.PagedMemorySize + $_.VirtualMemorySize + ` $_.WorkingSet)/1MB)}}
How it works...
In the first step, we simply execute Get-Process
to return all running processes named chrome
. This command returns basic process information such as the number of handles, the amount of memory resources, and the amount of CPU resources used by each process.
In the second step, we do the same as before, but this time telling the command to return all attributes for the processes. Dozens of attributes are returned, including details about the executable, threads, debugging information, and even when the process was started. Most importantly, this returns all available attributes for the process, thus allowing us to determine which attributes we are interested in returning in future queries.
The third step identifies specific attributes to return from the running processes. Any available process attribute can be included here in addition to or instead of the memory related attributes shown here.
The fourth step uses an expression to create a calculated result. In this situation, we create a calculated column named Total Memory
and add several of the memory related attributes together. Most mathematical functions such as multiplication and subtraction, and most textual commands such as append or replace, can be used as well.
The final step adds numeric formatting to the calculated column to make it more readable to the user. Two types of formatting are performed here:
The result is divided by
1 MB
(orX / 1,048,576
) to present the number in megabytes instead of bytesFormatting is applied to limit the resulting number to two decimal places