Introduction to PowerShell Count
The following article provides an outline for PowerShell Count. In our day to day activities, there are multiple scenarios in which we need to find out the count. It may range from how many files are stored in a location, number of active users, number of inactive users, number of error messages logged in a day or files greater than a date, etc. On all these scenarios the count can be found out using the Measure-Object or the count method.
Syntax of Measure Object
The measure-object cmdlet is used to calculate number of lines, characters and words in a file along with numerical properties of objects. It can perform three types of calculation based on the parameters that are available as part of a cmdlet. It can count the number of objects or objects of a certain category. It can perform arithmetic operations like minimum, maximum and average of numbers. If the object is of string value number of characters or words can be calculated.
Example:
Code:
Get-ChildItem -Path C:\vignesh\Test | Measure-Object -Property LastAccessTime
Output:
Parameters:
- -Allstats: This denotes that all the data of the mentioned properties. The data type of this parameter is switch parameter and default value is none. It doesn’t accept pipeline input and wild card characters are also not specified.
- -Average: This parameter calculates the average value of the mentioned properties. Its type is switch parameter. The default value is none. It doesn’t accept pipeline input and wild card characters are also not specified.
- -Character: This parameter returns the number of characters that are available in the specified input objects. Its type is switch parameter. The default value is none. It doesn’t accept pipeline input and wild card characters are also not specified.
- -IgnoreWhiteSpace: By default, white spaces are considered while calculating the number of characters, to avoid that this parameter is used. Its type is switch. The default value is none. It doesn’t accept pipeline input and wild card characters are also not specified.
- -InputObject: It denotes the objects to be measured. When this parameter is used, instead of passing this to the pipeline, the input objects is considered as a single entity. Its type is PSObject. The default value is none. Pipeline input is accepted whereas wild card characters are not permitted.
- -Line: This will return the number of lines in the specified input. Its type is switch parameter. The default value is none. It doesn’t accept pipeline input and wild card characters are also not permitted.
- -Maximum: This will return the maximum value of the mentioned properties or objects. Its type is switch parameter. The default value is none. It doesn’t accept pipeline input and wild card characters are also not permitted.
- -Minimum: This will return the minimum value of the mentioned properties or objects. Its type is switch parameter. The default value is none. It doesn’t accept pipeline input and wild card characters are also not permitted.
- -Property: This denotes the specified properties to measure. If no properties are specified all the properties are returned. The data type is PSPropertyExpression[]. Default value is none. Pipeline input is not allowed whereas wild card characters are permitted.
- -StandardDeviation: This will return the standard deviation value of the mentioned properties or objects. Its type is switch parameter. The default value is none. It doesn’t accept pipeline input and wild card characters are also not permitted.
- -Sum: This will return the total value of the mentioned properties or objects. Its type is switch parameter. The default value is none. It doesn’t accept pipeline input and wild card characters are also not permitted.
- -Word: This will return the total number of word present in the input. Its type is switch parameter. The default value is none. It doesn’t accept pipeline input and wild card characters are also not permitted.
Example of PowerShell Count
Given below is the example mentioned:
Code:
Write-Host "Welcome to powershell count demo"
Write-Host "number of files in a folder"
(Get-ChildItem -Path C:\vignesh -Recurse | Where-Object{ $_}).count
Write-Host "to count the total number of folders and subfolders in a path"
(Get-ChildItem -Path C:\vignesh -Recurse | Where-Object{ $_.PSIsContainer }).count
Write-Host "Number of warning in event viewer for the past 48 hours"
(Get-EventLog -LogName System -After ((Get-Date).AddDays(-2)) | Where-Object {$_.EntryType -eq "Warning"}).count
Write-Host "count using measure object command demo"
(Get-ChildItem -Path "C:\vignesh\test" | Measure-Object | Select-Object Count).count
Write-Host "number of files using measure object"
Get-ChildItem -Path C:\vignesh\Test -Recurse | Where-Object{ !($_.PSIsContainer) } | Measure-Object
Write-Host "Selecting only count"
Get-ChildItem -Path C:\Vignesh\Test\Actual1239 | Measure-Object | Select-Object Count
Write-Host "Number of files in each category"
Get-ChildItem -Path C:\vignesh | Group-Object Extension -NoElement
Write-Host "maximum size of a file in a path"
Get-ChildItem -Path c:\vignesh | Measure-Object -Property length -Maximum
Write-Host "minimum size of a file in a path"
Get-ChildItem -Path c:\vignesh | Measure-Object -Property length -Minimum
Write-Host "average size of a file in a path"
Get-ChildItem -Path c:\vignesh | Measure-Object -Property length -Average
Write-Host "total size of all files in a path"
Get-ChildItem -Path c:\vignesh | Measure-Object -Property length -Sum
Write-Host "number of lines in input file"
Get-ChildItem -Path C:\Vignesh\KB.txt |Measure-Object -Line
Write-Host "number of characters in input file"
Get-ChildItem -Path C:\Vignesh\KB.txt |Measure-Object -Character
Write-Host "number of words in input file"
Get-ChildItem -Path C:\Vignesh\KB.txt |Measure-Object -Word
Write-Host "number of characters in a text file excluding white space"
Get-ChildItem -Path C:\Vignesh\KB.txt |Measure-Object -IgnoreWhiteSpace -Character
Write-Host "counting a csv file"
Import-Csv C:\vignesh\July3.csv | Measure-Object -Property MailboxResolvedOwnerName -Maximum
Import-Csv C:\vignesh\July3.csv | Measure-Object -Property MailboxResolvedOwnerName -Minimum
Output:
Conclusion
Thus, in this article we saw about how count operation is performed in PowerShell on various objects, file objects and other types of commands. It showed the various ways using count object or Measure object cmdlet. The article demonstrated ample examples on count can be used in various scenarios.
Recommended Articles
This is a guide to PowerShell Count. Here we discuss the introduction to PowerShell Count along with example respectively. You may also have a look at the following articles to learn more –
2 Online Courses | 1 Hands-on Project | 4+ Hours | Verifiable Certificate of Completion
4.5
View Course
Related Courses