Mark Minasi's Tech Forum
Sign up Calendar Latest Topics

  Author   Comment  

Avatar / Picture

Senior Member
Posts: 301
Reply with quote  #1 

Why do I get '9' for this syntax ?  I would expect '5' since I've used '-first 5'

(Get-ADComputer -ldapfilter "(OperatingSystem=*server*)" -Properties *| Select-Object -First 5 | Format-table cn,OperatingSystem).count 

The syntax :
Get-ADComputer -ldapfilter "(OperatingSystem=*server*)" -Properties *| Select-Object -First 5 | Format-table cn,OperatingSystem

gives me 5 lines.

Pieter Demeulemeester

Avatar / Picture

Senior Member
Posts: 118
Reply with quote  #2 
Fun fun question ... wth?!?

I'm more of a powershell for sharepoint gal, so normally I'm really hampered by this weird world of we're-sharepoint-so-we-do-everything-different.  But for your question, I figured it would be a straight forward logic problem, so I tinkered a bit.  And it still seems like it is a logic problem, and that the answer to your question is four - not 42 (the answer to life, the universe, and everything), but four.  

So I set up a csv file with five columns, more than 400 rows, and did an import-csv.  I kept getting the same results you were finding ... no matter how many "-First " switches I used, the results were alwasy the # of rows plus four.

I thought maybe four represented the four bits of header components at the top of the table, you know?  

cn    OperatingSystem
---   ----------

But no... using -HideTableHeaders didn't fix that. And adding a third column of data to the request didn't change the mystery "plus four" to a "plus six."  

So I don't know why-four at this point, but if your primary purpose is to count the number of records returned, then dropping the Format-Table portion is what seems to fix the answer.  

(Get-ADComputer -ldapfilter "(OperatingSystem=*server*)" -Properties *| Select-Object -First 5).count

Should yield "five," or at least it seems to in my testing.


(Get-ADComputer -ldapfilter "(OperatingSystem=*server*)" -Properties).count

Should yield the actual number of computer ojbects where OS = server.  



Avatar / Picture

Senior Member
Posts: 421
Reply with quote  #3 
Format-Table converts whatever you feed it to lines of text. Apparently the table you get has nine lines, including the empty ones, thus the count of nine.
Evgenij Smirnov

My personal blog (German):
My stuff on PSGallery:

Posts: 502
Reply with quote  #4 
That is weird.  I tried both to a TXT file and both have empty lines. 

Good Catch CJ!!

Tim Bolton @jsclmedave
Email: [string](0..20|%{[char][int](32+('527377347976847978324785847679797514357977').substring(($_*2),2))}) -replace ' '  

New to the forum? Please Read this
Previous Topic | Next Topic

Quick Navigation:

Easily create a Forum Website with Website Toolbox.