Get the App
SLTechnology News&Howtos  ›  Servers  › 

Powershell utility commands (2)

Shulou Source: shulou.com Published: 2022-06-02 14:50:56 11月13日 Update

Here I go again. The practical command I share with you today is Measure-Object. If you look at the literal meaning of this command, you can see that it is used for statistical evaluation objects. Let's talk about its specific usage.

First of all, we can look at the syntax of this command and see which parameters it can support.

PS C:\ Windows\ system32 > Get-Command measure-Object-SyntaxMeasure-Object [[- Property]] [- InputObject] [- Sum] [- Average] [- Maximum] [- Minimum] [] Measure-Object [[- Property]] [- InputObject] [- Line] [- Word] [- Character] [- IgnoreWhiteSpace] []

From the above we can see that Measure-Object can support summation, average, take the maximum value, take the minimum value, count the number of lines, words, characters, IgnoreWhiteSpace parameter is to allow you to ignore the white space character (space, enter) count.

Let's use the code to verify these functions separately.

PS C:\ Windows\ system32 > Get-Process | measure-Property vm-MaximumCount: 210Average: Sum: Maximum: 2238980472832Minimum: VMPS C:\ Windows\ system32 > Get-Process | measure-Property vm-MinimumCount: 210Average: Sum: Maximum: Minimum: 4096Property: VMPS C:\ Windows\ system32 > Get-Process | measure-Property vm-AverageCount: 214Average: 1596722333006.95Sum: Maximum: Minimum: Property: VMPS C:\ VMPS\ VMPS > VMPS | | measure-Property vm-sumCount: 212Average: Sum: 337291811336192Maximum: Minimum: Property: VMPS C:\ Windows\ system32 > Get-Content D:\ File1.txt | measure-Line-Word-CharacterLines Words Characters Property- 4 20 113 PS C:\ Windows\ system32 > Get-Content D:\ File1.txt | measure-Line-Word-| Character-IgnoreWhiteSpaceLines Words Characters Property- 4 20 97 PS C:\ Windows\ system32 > Get-Content D:\ File1.txtLearning Powershell TechnologyI like Powershell Script LanguageDo you like it as meFine Let's learn it togather

The above code shows the function of Measure-Object. I ask you whether it is strong and powerful. You don't need to call max and min for a while, and you can get all done with one command.

Let's introduce another practical Measure command, Measure-Command, which is often used to detect the running time of code. Through this time, we can evaluate which kind of code is better and take less time, improve the execution efficiency of our code, and reduce the performance loss of the host running the code.

$Script1= {For ($iposit / I-le 100 +) {Add-Content-Path D:\ File1.txt-Value "I want add some word in this file"} $Script2= {$NW=New-Object System.IO.StreamWriter "d:\ file1.txt"; For ($NW.WriteLine / "I Write it again!") {$NW.WriteLine $nw.Close ()} PS C:\ Windows\ system32 > Measure-Command-Expression $Script1 | select TotalMillisecondsTotalMilliseconds- 117.5692PS C:\ Windows\ system32 > Measure-Command-Expression $Script2 | select TotalMillisecondsTotalMilliseconds- 2.5188

The same result can be evaluated through Measure-Command, but the time spent is completely different because the code is different, which makes us know whether our code is good or not.

All right, that's all for today. Guazi Peanut Beer, guest?

Tags: Code command time statistics evaluation practical excellent function parameter character support run different powerful maximum minimum powerful host word beer Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Redmi Microsoft Shulou Information NVidia Shulou Tech Info