Skip to main content
infervour.com

Back to all posts

How to Count Objects In PowerShell?

Published on
4 min read
How to Count Objects In PowerShell? image

Best PowerShell Scripting Guides to Buy in October 2025

1 PowerShell for Sysadmins: Workflow Automation Made Easy

PowerShell for Sysadmins: Workflow Automation Made Easy

  • MASTER POWERSHELL FOR EFFORTLESS WORKFLOW AUTOMATION.
  • PRACTICAL TIPS TAILORED FOR SYSADMINS TO BOOST PRODUCTIVITY.
  • ACCESSIBLE PAPERBACK FORMAT FOR EASY REFERENCE AND LEARNING.
BUY & SAVE
$23.99 $39.99
Save 40%
PowerShell for Sysadmins: Workflow Automation Made Easy
2 Learn PowerShell in a Month of Lunches, Fourth Edition: Covers Windows, Linux, and macOS

Learn PowerShell in a Month of Lunches, Fourth Edition: Covers Windows, Linux, and macOS

BUY & SAVE
$39.99
Learn PowerShell in a Month of Lunches, Fourth Edition: Covers Windows, Linux, and macOS
3 Mastering PowerShell Scripting: Automate repetitive tasks and simplify complex administrative tasks using PowerShell

Mastering PowerShell Scripting: Automate repetitive tasks and simplify complex administrative tasks using PowerShell

BUY & SAVE
$26.62 $49.99
Save 47%
Mastering PowerShell Scripting: Automate repetitive tasks and simplify complex administrative tasks using PowerShell
4 PowerShell Cookbook: Your Complete Guide to Scripting the Ubiquitous Object-Based Shell

PowerShell Cookbook: Your Complete Guide to Scripting the Ubiquitous Object-Based Shell

BUY & SAVE
$49.49 $89.99
Save 45%
PowerShell Cookbook: Your Complete Guide to Scripting the Ubiquitous Object-Based Shell
5 PowerShell Pocket Reference: Portable Help for PowerShell Scripters

PowerShell Pocket Reference: Portable Help for PowerShell Scripters

BUY & SAVE
$18.99 $29.99
Save 37%
PowerShell Pocket Reference: Portable Help for PowerShell Scripters
6 PowerShell Automation and Scripting for Cybersecurity: Hacking and defense for red and blue teamers

PowerShell Automation and Scripting for Cybersecurity: Hacking and defense for red and blue teamers

BUY & SAVE
$32.60 $49.99
Save 35%
PowerShell Automation and Scripting for Cybersecurity: Hacking and defense for red and blue teamers
7 Scripting: Automation with Bash, PowerShell, and Python—Automate Everyday IT Tasks from Backups to Web Scraping in Just a Few Lines of Code (Rheinwerk Computing)

Scripting: Automation with Bash, PowerShell, and Python—Automate Everyday IT Tasks from Backups to Web Scraping in Just a Few Lines of Code (Rheinwerk Computing)

BUY & SAVE
$36.76 $49.95
Save 26%
Scripting: Automation with Bash, PowerShell, and Python—Automate Everyday IT Tasks from Backups to Web Scraping in Just a Few Lines of Code (Rheinwerk Computing)
8 Powershell for Beginners A Step-by-Step Guide to Learning Scripting, Cmdlets: Learn PowerShell Basics, Automate IT Tasks, and Boost Productivity with Clear Examples and Practical Exercises

Powershell for Beginners A Step-by-Step Guide to Learning Scripting, Cmdlets: Learn PowerShell Basics, Automate IT Tasks, and Boost Productivity with Clear Examples and Practical Exercises

BUY & SAVE
$24.99
Powershell for Beginners A Step-by-Step Guide to Learning Scripting, Cmdlets: Learn PowerShell Basics, Automate IT Tasks, and Boost Productivity with Clear Examples and Practical Exercises
9 Windows PowerShell in Action

Windows PowerShell in Action

  • BRAND NEW: UNOPENED QUALITY GUARANTEED FOR CUSTOMER SATISFACTION.
  • COMPLETE SET: INCLUDES ALL ESSENTIAL ACCESSORIES FOR IMMEDIATE USE.
  • SECURE SHIPPING: FAST DELIVERY ENSURES YOUR PURCHASE ARRIVES SAFELY.
BUY & SAVE
$59.99
Windows PowerShell in Action
10 Learn PowerShell Scripting in a Month of Lunches, Second Edition: Write and organize scripts and tools

Learn PowerShell Scripting in a Month of Lunches, Second Edition: Write and organize scripts and tools

BUY & SAVE
$47.34 $59.99
Save 21%
Learn PowerShell Scripting in a Month of Lunches, Second Edition: Write and organize scripts and tools
+
ONE MORE?

To count objects in PowerShell, you can use the Measure-Object cmdlet. This cmdlet provides various ways to calculate the count, length, minimum, maximum, sum, or average of numeric properties in objects. Here's an example of how to count objects using PowerShell:

  1. Start by opening PowerShell.
  2. Create or obtain a collection of objects that you want to count. For example, you might have an array of strings like this:

$myArray = "Apple", "Banana", "Cherry", "Date"

  1. Use the Measure-Object cmdlet with the -InputObject parameter to specify the object or collection you want to count. In this case, we'll count the elements in $myArray:

$objectCount = $myArray | Measure-Object

  1. You can now access the count of objects by referencing the Count property of the result:

$objectCount.Count

This will output the total count of objects, in this case, 4.

Note: You can also count objects based on specific properties using the -Property parameter of Measure-Object. For example, if you have a collection of custom objects with a property called "Size", and you want to count objects with a "Size" greater than 10, you can use something like:

$customObjects = [pscustomobject]@{Name="Obj1"; Size=5}, [pscustomobject]@{Name="Obj2"; Size=12}, [pscustomobject]@{Name="Obj3"; Size=8} $filteredCount = $customObjects | Measure-Object -Property Size -GT 10 $filteredCount.Count

This will output the count of objects with "Size" greater than 10.

Remember that Measure-Object is a versatile cmdlet that allows you to perform various calculations on objects in PowerShell, not just counting them.

How do I count the number of lines in a multi-line string using PowerShell?

To count the number of lines in a multi-line string using PowerShell, you can use the [split()](https://aryalinux.org/blog/how-to-use-regex-to-match-split-characters-in-java) method combined with the count property. Here's an example:

$multilineString = @" This is a multi-line string. "@

$lineCount = $multilineString.Split("`n").Count Write-Host "Number of lines: $lineCount"

In this example, the multi-line string is assigned to the variable $multilineString. Using the split() method, the string is split by the line break character ( n) and converted into an array of lines. The countproperty is then used to get the number of items (lines) in the resulting array. Finally, the line count is displayed using theWrite-Host` cmdlet.

How to count the number of characters in a variable using PowerShell?

To count the number of characters in a variable using PowerShell, you can use the .Length property of the variable. Here's an example:

$myString = "Hello, World!" $characterCount = $myString.Length Write-Host "Number of characters: $characterCount"

Output:

Number of characters: 13

In the above example, $myString is the variable containing the string, and $myString.Length gives the number of characters in the string, which is then stored in the $characterCount variable. Finally, the Write-Host cmdlet is used to display the number of characters.

How to count files with a specific file extension using PowerShell?

To count files with a specific file extension using PowerShell, you can use the following command:

$files = Get-ChildItem -Path "C:\FolderPath" -Filter "*.extension" $count = $files.Count Write-Host "Total files with .extension: $count"

Here are the steps to use this command:

  1. Replace "C:\FolderPath" with the actual path of the folder where you want to count the files.
  2. Replace ".extension" with your desired file extension. For example, if you want to count files with the ".txt" extension, replace ".extension" with ".txt".
  3. Run the PowerShell command.

The command will retrieve all the files with the specified file extension from the provided folder path, store them in the $files variable, and then use the Count property on the variable to get the total count. Finally, it will display the count using the Write-Host command.

How can I count the number of network adapters on a computer using PowerShell?

You can use the Get-NetAdapter cmdlet in PowerShell to count the number of network adapters on a computer. Here's how you can do it:

  1. Open PowerShell by searching for "PowerShell" in the Start menu.
  2. Type the following command and press Enter:

Get-NetAdapter | Measure-Object

  1. PowerShell will display the count of network adapters on your computer.

Alternatively, you can store the count in a variable for further processing or display. Here's an example:

$netAdapters = Get-NetAdapter $adapterCount = $netAdapters.Count Write-Host "Number of network adapters: $adapterCount"

In this example, the count of network adapters is stored in the $adapterCount variable and displayed using the Write-Host cmdlet.