Download Mastering-PowerShell
Transcript
Use the option described in Chapter 5: with Format-Table or FormatList to output calculated columns. This allows you to select from
current properties of a Process object, as well as properties of child
objects, and you can also obtain or calculate entirely new data.
In the following example, Get-Process returns for each process not only its name
but also the directory from which the process was started, as well as a
description of the process. The start directory is a property of a child object in
MainModule. The static .NET method GetVersionInfo() will obtain a description of
the process if it is given the path of the process. That can be found in the Path
property:
Get-Process | Format-Table Name, @{ex={ $_.MainModule.FileName };
la="StartDirectory"},
@{ex={([system.diagnostics.fileversioninfo]::`
GetVersionInfo($_.Path)).FileDescription}; la="Description"} -wrap
Name
---agrsmsvc
AppSvc32
Ati2evxx
ATSwpNav
BatteryMiser5
ccApp
StartDirectory
----------C:\Windows\system32\
agrsmsvc.exe
C:\Program Files\Common
Files\Symantec Shared\App
Core\AppSvc32.exe
C:\Windows\system32\
Ati2evxx.exe
C:\Program Files\Finger
print Sensor\ATSwpNav
.exe
C:\Program Files\LG
Software\Battery Miser\
BatteryMiser5.exe
C:\Program Files\Common
Files\Symantec Shared\
ccApp.exe
Description
-----------Agere Soft Modem Call
Progress Service
Symantec Application
Core Service
ATI External Event
Utility EXE Module
ATSwpNav Application
Battery Miser
Symantec User Session
(...)
Counting Processes
Processes can be counted rather easily because the result of Get-Process is (nearly) always an array,
which comes with the Count property. Get-Process won't necessarily return the result as an array;
only if the result is a single process or no process at all. That's why you should always first convert
the result into an array before you determine the number of array elements:
# Determine the number of notepads:
@(Get-Process notepad).Count
1
Table of Contents | About PowerShell Plus
493
Sponsors | Resources | © BBS Technologies