EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 360+ Courses All in One Bundle
  • Login
Home Data Science Data Science Tutorials PowerShell Tutorial PowerShell Count
Secondary Sidebar
PowerShell Tutorial
  • cmdlet
    • PowerShell Wait
    • PowerShell Match
    • cmdlets in PowerShell
    • Start PowerShell from cmd
    • Add-Content in PowerShell
    • Get Help in PowerShell
    • PowerShell Copy-Item
    • PowerShell Remove-Item
    • PowerShell Move-Item
    • Get Command in PowerShell
    • PowerShell Run Command
    • Windows PowerShell ISE
    • Windows Powershell Commands
    • WinRM PowerShell
    • PowerShell Date
    • Powershell Write-Host
    • PowerShell Get-ChildItem
    • PowerShell Sort-Object
    • PowerShell Where Object
    • PowerShell Set-Content
    • PowerShell Set-Location
    • PowerShell Invoke-Command
    • PowerShell Invoke-Webrequest
    • PowerShell Get-Location
    • PowerShell Get-Date
    • PowerShell Get-Service
    • PowerShell Test-Path
    • Powershell Module Path
    • PowerShell Out-File
    • PowerShell if File Exists
    • Powershell Copy File
    • PowerShell Delete File
    • PowerShell New-Item
    • PowerShell Rename-Item
    • PowerShell ComputerName
    • PowerShell Get-Content
    • PowerShell Get-Item
    • PowerShell Get-ADUser
    • PowerShell Grep
    • PowerShell Concatenate String
    • PowerShell Get-Process
    • PowerShell Count
    • PowerShell pause
  • Basics
    • PowerShell Restart Service
    • PowerShell comment
    • PowerShell Map Network Drive
    • PowerShell Append to File
    • PowerShell print
    • What is PowerShell
    • Uses Of Powershell
    • PowerShell Empire
    • PowerShell Parameter
    • PowerShell Stop Service
    • PowerShell Versions
    • How To Install PowerShell
    • PowerShell uninstall module
    • How to Use PowerShell?
    • PowerShell Logging
    • PowerShell Tools
    • PowerShell Commands
    • PowerShell Version Command
    • PowerShell Administrator
    • PowerShell Modules
    • PowerShell Registry
    • PowerShell block Comment
    • PowerShell Verbs
    • PowerShell list
    • PowerShell add user to group
    • PowerShell Write to Console
    • Variable in PowerShell
    • PowerShell New Line
    • PowerShell prompt for input
    • PowerShell File Extension
    • Powershell Remotesigned
    • PowerShell Write to File
    • PowerShell Ping
    • PowerShell wget
    • PowerShell Global variable
    • PowerShell Get-ADGroup
    • Array in PowerShell
    • PowerShell Multidimensional Array
    • PowerShell Array of Strings
    • PowerShell? join array
    • Useful PowerShell Scripts
    • String in PowerShell
    • PowerShell Switch Statement
    • PowerShell Function Parameters
    • PowerShell vs PowerShell ISE
    • PowerShell test-connection
    • PowerShell Test-NetConnection
    • PowerShell GUI
    • PowerShell Variable in String
    • PowerShell Active Directory
  • Variables
    • PowerShell Variables
    • PowerShell Environment Variables
    • PowerShell set environment variable
    • Hashtable in PowerShell
    • Set Variable in PowerShell
  • Operators
    • PowerShell Operators
    • Comparison Operators in PowerShell
    • Logical Operators in PowerShell
    • PowerShell Boolean
    • PowerShell Like Operator
  • Control Statements
    • If Statement in PowerShell
    • If Else in PowerShell
    • Else If in PowerShell
    • Loops in PowerShell
    • For loop in PowerShell
    • PowerShell While Loop
    • PowerShell do while
    • PowerShell Loop through Array
    • PowerShell add to array
    • PowerShell ForEach Loop
    • PowerShell Break
    • PowerShell Continue
    • Switch Case in PowerShell
    • PowerShell If-Not
    • Try-catch in PowerShell
  • Functions
    • PowerShell Functions
    • PowerShell String Functions
    • powershell nslookup
    • PowerShell here string
    • PowerShell Wildcards
    • Regex in PowerShell
    • PowerShell not like
    • PowerShell Filter
    • PowerShell Sleep
    • PowerShell where
    • PowerShell join string
    • PowerShell Exit
    • PowerShell null
    • PowerShell Dictionary
    • PowerShell Location
    • PowerShell Start-Service
    • PowerShell is not digitally signed
    • PowerShell Uptime
    • PowerShell Create Directory
    • PowerShell Trim
    • PowerShell Join-Path
    • PowerShell Execution Policy
    • PowerShell SubString
    • PowerShell Format Table
    • PowerShell Import Module
    • PowerShell ForEach Object
    • PowerShell Alias
    • PowerShell Scheduled Task
    • PowerShell Convert String to Date
    • PowerShell Split String
    • PowerShell Multiline String
    • PowerShell MultiLine Comment
    • PowerShell Rename Folder
    • PowerShell Delete Folder
    • PowerShell String Replace
    • PowerShell join
    • PowerShell xcopy
    • PowerShell Base64
    • PowerShell Tail
    • PowerShell User List
    • PowerShell remove User from group
    • PowerShell JSON Format
    • PowerShell Send Mail
    • PowerShell Convert to String
    • PowerShell Start-Process
    • PowerShell change directory
    • PowerShell Open File
    • PowerShell Batch File
    • PowerShell ZIP
    • PowerShell unzip
    • PowerShell XML
    • PowerShell XML Parsing
    • Remote PowerShell
    • PowerShell Escape Character
    • PowerShell scriptblock
    • PowerShell Executable Location
    • PowerShell Import-CSV?
    • PowerShell Export CSV
  • Interview Questions
    • PowerShell Interview Questions

Related Courses

Shell Scripting Course

All in One Data Science Courses

Data Visualization Courses

PowerShell Count

By Priya PedamkarPriya Pedamkar

PowerShell Count

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:

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

Get-ChildItem -Path C:\vignesh\Test | Measure-Object -Property LastAccessTime

Output:

PowerShell Count 1

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:

All in One Data Science Bundle(360+ Courses, 50+ projects)
Python TutorialMachine LearningAWSArtificial Intelligence
TableauR ProgrammingPowerBIDeep Learning
Price
View Courses
360+ Online Courses | 50+ projects | 1500+ Hours | Verifiable Certificates | Lifetime Access
4.7 (86,527 ratings)

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:

number of files in a folder

PowerShell Count 3

PowerShell Count 4

number of characters in input file

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 –

  1. Else If in PowerShell
  2. Windows PowerShell ISE
  3. PowerShell Send Mail
  4. PowerShell Functions
Popular Course in this category
PowerShell Training (2 Courses, 1 Project)
  2 Online Courses |  1 Hands-on Project |   4+ Hours |  Verifiable Certificate of Completion
4.5
Price

View Course

Related Courses

Shell Scripting Training (4 Courses, 1 Project)4.9
All in One Data Science Bundle (360+ Courses, 50+ projects)4.8
Data Visualization Training (15 Courses, 5+ Projects)4.7
0 Shares
Share
Tweet
Share
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2022 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Data Science Course

SPSS, Data visualization with Python, Matplotlib Library, Seaborn Package

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more