Use Measure-object and Compare-object to count and contrast pipe results. Measure-object allows you to specify the properties of the object to be counted. Compare-object can compare snapshots before and after an object.
Statistics and calculations
You can use Measure-object to find the minimum, maximum, average, and the properties of an object. For example, to see where the current directory file occupies space.
PS c:powershell> ls | Measure length
Count :
Average:
Sum :
Maximum:
Minimum:
property:length
PS c:powershell> ls | Measure Length-average-sum-maximum-minimum
Count :
average:53768.8421052632
Sum : 1021608
maximum:735892
minimum:0
property:length
You can also use Measure-object to count the number of characters in a text file, the number of words, the number of lines
For example, we can save the following text to: Word.txt.
Retirement anxiety spreads among the one Percent
report:green Monday a Boon for Online Shopping
5 Lesser-known Wa Ys to Boost Your Score
PS c:powershell> get-content. Word.txt | measure-line-word-character Lines
Wor DS Characters Property
----------------------------
3 141
Compare objects
There are times when you need to compare the two periods before and after which processes are turned on, and how the service status changes. A job like this can be handed over to Compare-object.
Compare different time periods
You can save all open process information snapshots to a variable for a period of time, save a new snapshot of the process, and then compare it to the compare-object.
PS c:powershell> $before =get-process
PS c:powershell> $after =get-process
PS c:powershell> Compare-object $before $after
inputobject sideindicator
----------- -------------
System.Diagnostics.Process (Notepad) =>
System.Diagnostics.Process (notepad) =>
System.Diagnostics.Process (ACRORD32)
$before is an array that stores all of the current process objects, and the result of the Compare-object is two columns: Inputobject is an inconsistent object, and Sideindicator represents the new object for the inconsistency,=>. In combination with the example above, 3 processes (Acrord32,acrord32,prevhost) have been closed in the before and after periods, and 2 processes have been opened (Notepad,notepad).
Check for changes in objects
Compare-object does not only compare the addition and decrease of objects in an object group, it can also compare the property changes of each object because it has a parameter-property.
PS c:powershell> get-service wsearch
Status Name DisplayName
------ ---- -----------
Running Wsearch Windows Search
PS c:powershell> $svc 1=get-service wsearch
PS c:powershell> $svc 1.stop ()
PS C: powershell> $svc 2=get-service wsearch
PS c:powershell> compare-object $svc 1 $svc 2-property status,name
Status Name Sideindicator
---------- -------------
startpending wsearch =>
Running wsearch
Compare the contents of a file
The text file can be read by get-content and the file is saved as an array in a behavior unit, and can still be compared by compare-object. The following example creates two different text files and then compares the get-content results of two files through Compare-object.
PS c:powershell> "Hellow
>> power
>> Shell" >a.txt
>>
PS c:powershell> " Hollow
>> Shell
>> Linux ">b.txt
>>
PS c:powershell> compare-object ( Get-content a.txt) (get-content. B.txt)
inputobject sideindicator
------------------------
Hollow =>
Linux =>
hellow
Save snapshots for later use
The example above is to save the object in a variable, and a disadvantage of the variable is that once the PowerShell exits or the computer shuts down the variable disappears. So the best way to do this is to save the object to a disk file. How to serialize objects into a single file, PowerShell provides a command: Export-clixml, you can do this, and a deserialized command import-clixml. This will make Compare-object's commands more convenient. For example, one months ago to save a $before object, one months after the comparison can be.
PS c:powershell> get-process | Export-clixml before.xml
PS c:powershell> $before =import-clixml. Before.xml
ps c:powershell> $after = Get-process
PS c:powershell> compare-object-referenceobject $before-differenceobject $after