Skip to content
Menu
ScriptingNerd
  • Home
  • Quick Tips
  • GitHub
ScriptingNerd

Powershell counting to $null instead of 1

Posted on April 19, 2022April 20, 2022 by Patrik

There is this weird thing in Powershell that you might have come across if you have ever counted how many objects you have. In some cases Powershell will count 0 $null 2 3…

You could say that this is a side effect of how Powershell tries to "magically" simplify stuff. The thing is, it doesn't always happen, instead it depends on what it is you want to count. As an example it will happen when counting ADUsers but it won't happen when counting processes. In the example below there is a user called Patrik yet the count returns nothing, well, in reality it returns $null.

PS C:\> (Get-ADUser patrik -Properties DisplayName).DisplayName
Patrik Johansson

PS C:\> (Get-ADUser -Filter { DisplayName -like "*Patrik*" }).count

PS C:\> (Get-ADUser -Filter { DisplayName -like "*Karl*" }).count
0

PS C:\> (Get-ADUser -Filter { DisplayName -like "*Anders*" }).count
2

PS C:\>

This could lead to some very unfortunate results if it where to happen in your script. Fortunately there is a very easy fix for it. Simply adding a @ before the parentheses forces Powershell to see it as an array and then it will count it just fine.

PS C:\> @(Get-ADUser -Filter { DisplayName -like "*Patrik*" }).count
1
 
PS C:\>

This is one of those weird things that after a while will come natural for you but that you will miss over and over in the beginning.

Happy scripting!

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

About

  • Patrik Johansson
    Professional System Administrator

    Working with a wide flora of systems but with a focus on Configuration Manager (MEMCM/SCCM).

    Passionate about Powershell and building tools that are easy to use for everyone.
    In my spare time I work on other coding projects as well, mainly Booksonic

    LinkedIn Resume GitHub Blog

Recent Posts

  • Powershell counting to $null instead of 1
  • Running ForEach in parallel on Windows Powershell 5 (and older)
  • Use Powershell to create a "fake" program in the programs and features list for indexing purposes
  • How to read the manifest of an appx package file using Powershell
  • Building Powershell GUIs that won't freeze when you use them

Archives

  • April 2022
  • November 2021
  • March 2020
  • December 2019
  • February 2019

Categories

  • Powershell
  • Quick Tips

Affiliates

  • These are affiliate links, if you click on them and buy something I may earn a small amount of money without any additional cost to you. Any links found here are for products I am using myself and can vouch for.
  • InterServer
    Very cheap SSD/HDD based virtual servers (VPS). Starting at $6/month for 1TB. Use coupon code BOOKSONIC and get the first month for just 1 cent. I have recently started using intersever for personal projects and so far I am very happy with them.
  • Contabo
    Cheap HDD/SSD/NVMe based Virtual servers (VPS)
    I have used Contabo for years both for my private needs as well as to host the booksonic demo server
©2022 ScriptingNerd | Powered by WordPress & Superb Themes