How to Count Objects in PowerShell

-

|

Introduction

There are so many reasons you may want to count objects in PowerShell. As an example, you may want to count error messages in an event log. Or count the number of folders in a specific folder.

Whatever you need to count in PowerShell you can use either the count method or Measure-Object cmdlet. This guide shows ways you can count with both methods.

Advertisement

How to Count Objects in PowerShell with Count Method

The count method of an object can be accessed by wrapping the object in a bracket (), followed by a dot then the word, count.

Here is the general syntax

 (object to count).count

As an example, the Get-ChildItem command returns the list of files and folders in drive c:\

How to Count Objects in PowerShell with Count Method

To use PowerShell count method to count the number of items, simply use the command below:

(Get-ChildItem).count

Here is the result:

How to Count Objects in PowerShell with Count Method

You can also use PowerShell count method to count only folders (directories).

The command below will count the total number of folders and subfolders in C:\G-Drive\

(Get-ChildItem -Path C:\G-Drive\ -Recurse | Where-Object{ $_.PSIsContainer }).count

Here is the result with PowerShell count method

If you want to count the number of files instead of folders, add the ! operator in the Where-Object command as shown below:

(Get-ChildItem -Path C:\G-Drive\ -Recurse | Where-Object{ !($_.PSIsContainer) }).count

By adding !, you tell PowerShell to return objects that are NOT directories. Here is the result.

The result shows that there are 35,811 files.

Another way PowerShell count can be useful is in getting the number of error messages in an event log.

Say you want to know how many error messages are in the System log in the last 24 hours, the PowerShell count command below will do the job:

(Get-EventLog -LogName System -After ((Get-Date).AddDays(-1)) | Where-Object {$_.EntryType -eq "Error"}).count

How to Count Objects in PowerShell with Measure-Object

Another way to use PowerShell count is to use the Measure-Object cmdlet. The syntax for this method is to pip the output of the result you wish to count to the Measure-Object cmdlet.

command with object to count | Measure-Object

Following the same examples from the previous section.

To count the number of items in drive c:\, use the command below:

Get-ChildItem | Measure-Object

The result is shown below

How to Count Objects in PowerShell with Measure-Object

If you want to show just the count, adjust the command as shown below:

(Get-ChildItem | Measure-Object | Select-Object Count).count

Here is the PowerShell count result:

In another example, we will count the number of folders and subfolders in the path C:\G-Drive\ using Measure-Object command.

Here is the command:

Get-ChildItem -Path C:\G-Drive\ -Recurse | Where-Object{ $_.PSIsContainer } | Measure-Object

To count files instead of directories (folders), use the command below:

Get-ChildItem -Path C:\G-Drive\ -Recurse | Where-Object{ !($_.PSIsContainer) } | Measure-Object

Here is the result of the above command in PowerShell

Finally, to count how many error messages are in the System log in the last 24 hours, use the PowerShell count command below:

Get-EventLog -LogName System -After ((Get-Date).AddDays(-1)) | Where-Object {$_.EntryType -eq "Error"} | Measure-Object

Here is the result

Advertisement

Conclusion

In PowerShell you can count with the count method or Measure-Object cmdlet.

As a quick recount; to count with the count, wrap the object in (), followed by a dot then the word count. To count with Measure-Object cmdlet, pip the object to the Measure-Object cmdlet

(object to count).count

command with object to count | Measure-Object

It is that simple!

If you have any question or comment use the “Leave a Reply” form at the end of this page. You could also share your experiencing counting objects in PowerShell.

Other Helpful Guides

Additional Resources and References

LEAVE A REPLY

Please enter your comment!
Please enter your name here

FEATURED POSTS

How to Share a Folder in Windows 10 (3 Methods)

Introduction This guide demos how to share folder in windows 10. It covers 3 methods. Options...

How to Map Network Drive in Windows 10 (5 Methods)

Introduction This guide demos 5 methods to Map Network Drive in Windows 10. Options to...
How to Install Windows 10 1909 Preview Build

How to Install Windows 10 19H2 Preview Build

Introduction If you are a member of Windows 10 Insider Program you can install Windows 10 19H2 Preview Build....

How to Sign in to Windows 10 with a Microsoft Account

Introduction When you installed Windows 10 you may have created and signed in with a local account. You can...
Disable IPv6 in Windows 10

How to Disable IPv6 in Windows 10 (3 Methods)

Introduction This guide demos 3 methods to disable IPv6 in Windows 10: Disable IPv6 from...

Advertisement

TRENDING POSTS

Remote Desktop Connection

Remote Desktop Connection an Internal Error Has Occurred [Fixed]

Introduction I recently received the error message "Remote Desktop Connection an Internal Error Has Occurred". It was strange because...

Find My Samsung: Register and Use Samsung Find my Mobile

Introduction Ever wondered how you could find your Samsung phone if you lost it? Find my Samsung or Samsung...
What is the Difference Between PowerShell and CMD?

Windows Powershell vs CMD: Differences and Similarities

Introduction This short guide compares Windows PowerShell vs CMD (Windows command prompt). I will cover the history and nature...
Spotify No Longer Supports this Version of Microsoft Edge

Spotify No Longer Supports this Version of Microsoft Edge [Fixed]

Introduction When you open Spotify web player on Microsoft Edge, you may receive the error message "Spotify No Longer...
Windows 10 Won't Boot

Windows 10 Won’t Boot With Black Screen? 3 Ways to Fix It

Why Won't Windows 10 Boot Up? If your Windows 10 stops with a black screen, the first question in...

Advertisement

BEST OF ITECHGUIDES

scanning and repairing drive

Scanning and Repairing Drive Issue on Windows 10 [Fixed]

Introduction A number of Windows 10 users have reported that when they boot their computer it is stuck on...
Powershell_Arrays

PowerShell Array and Add to Array: Applications and Examples

What is a PowerShell Array? A PowerShell array is a data structure that stores a collection of items. The...
NTFS Permissions with Share Permissions

NTFS Permissions with Share Permissions Compared

Introduction Combing NTFS permissions with share Permissions increases folder security within a network. Every Windows Administrator should have a...
roll back windows 10 update

How to Roll Back Windows 10 Update (3 Methods)

Introduction There could be a number of reasons you may want to roll back windows 10 update. One common...
Error Code 0xc004fc07 in Server 2016 - "We Can't activate Windows on the device right now."

How to Fix “We Can’t Activate Windows on this Device right Now”

Introduction When you try to activate Windows Server 2016 with the GUI tool you may receive error code 0xc004fc07...

RECENT POSTS

How to Enable Hyper-V in Windows 10 (3 Methods)

How to Enable Hyper-V in Windows 10 (3 Methods)

Introduction This guide demos 3 methods to enable Hyper-V in Windows 10. To install Hyper-V...
RSAT Tools in Windows 10 Explained: Plus How to Install RSAT

RSAT Tools in Windows 10 Explained: Plus How to Install RSAT

Introduction Starting from October 2018 (1809) update, RSAT Tools became part of Windows 10. From this version of Windows...

How to Enable RSAT for Active Directory in Windows 10 (3 Methods)

Introduction This guide demos 3 methods to enable Active Directory in Windows 10. It is not exactly enabling "Active...
How to Install Windows 10 1909 Preview Build

How to Install Windows 10 1909 (19H2) Preview Build

Introduction Windows 10 1909 Preview is available for Windows Insiders. Made available early September, 2019 you have to be...
How to Install RSAT in Windows 10 (3 Methods)

How to Install RSAT in Windows 10 (3 Methods)

Introduction This guide demos how to Install RSAT in Windows 10. Starting with Windows 10...

How to Share a Folder in Windows 10 (3 Methods)

Introduction This guide demos how to share folder in windows 10. It covers 3 methods. Options...
Configure Map Network Drive with Group Policy

Map Network Drive in Windows 10 with Group Policy

Introduction This guide demos how to map network drive with group policy. This guide is...

How to Map Network Drive in Windows 10 (5 Methods)

Introduction This guide demos 5 methods to Map Network Drive in Windows 10. Options to...
How to Download Windows 10 ISO with Media Creation Tool

How to Download Windows 10 ISO with Media Creation Tool

Introduction This guide demos the steps to download Windows 10 ISO. You can download Windows 10 ISO with Media...

How to Install Windows 10 from Network Boot (Via WDS Server)

Introduction This guide demos how to install Windows 10 from network boot. The steps discussed in...

Advertisement

MUST READ

How to Cancel Spotify Premium

How to Cancel Spotify Premium

Introduction You can cancel Spotify Premium within the 30-day trial. If you cancel Spotify Premium within the trial period,...
Windows defender blocked by Group Policy

Windows Defender Blocked by Group Policy [Fixed]

Introduction If you receive the error message Windows Defender is blocked by group policy, your computer may have been...
How to Install and Configure DNS on Windows Server 2016

How to Install and Configure DNS on Windows Server 2016

Introduction This guide demos how to install and configure DNS on windows server 2016. Steps...
Windows could not automatically detect this network's proxy settings

How to Fix “Windows Could not Automatically Detect this Network’s Proxy Settings”

Introduction You suddenly lose the ability to connect to the internet. Then you run network troubleshooter and it returns...
Powershell Substring

How to Extract a Powershell Substring from a String

Introduction A substring is a part of a string. You can create a PowerShell substring from a string using...

By using this website you agree to accept our Privacy Policy and Terms & Conditions