Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8770927
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T17:39:33+00:00 2026-06-13T17:39:33+00:00

I have the following powershell script which binds to an active directory OU and

  • 0

I have the following powershell script which binds to an active directory OU and lists the computers. It seems to work fine except that it ouputs an extra 0 – I’m not sure why. Can anyone help?

 $strCategory = "computer"

 $objDomain = New-Object System.DirectoryServices.DirectoryEntry("LDAP://          OU=Computers,OU=datacenter,DC=ourdomain,DC=local")

 $objSearcher = New-Object System.DirectoryServices.DirectorySearcher($objDomain)
 $objSearcher.Filter = ("(objectCategory=$strCategory)")

  $colProplist = "name"
  foreach ($i in $colPropList){$objSearcher.PropertiesToLoad.Add($i)}

 $colResults = $objSearcher.FindAll()

 foreach ($objResult in $colResults)
 {
 $objComputer = $objResult.Properties; 
 $objComputer.name
 }

Output:
0
Server1
Server2
Server3

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T17:39:34+00:00Added an answer on June 13, 2026 at 5:39 pm

    You need to capture (or ignore) the output of the PropertiesToLoad.Add method, otherwise you’ll get a value for each property in $colPropList.

    foreach ($i in $colPropList){[void]$objSearcher.PropertiesToLoad.Add($i)}
    

    You can simplify and shorten your script and load a bunch of properties in one call without using a foreach loop. Another benefit of the AddRange method is that it doesn’t output the length of requested properties, so there’s no need to capture anything.

    $strCategory = "computer"
    $colProplist = "name","distinguishedname"
    
    $searcher = [adsisearcher]"(objectCategory=$strCategory)"
    $searcher.PropertiesToLoad.AddRange($colProplist)
    $searcher.FindAll() | Foreach-Object {$_.Properties}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have following script that executes all the .reg files in the current directory.
I have a powershell script in which I do the following $somePSObjectHashtables = New-Object
I have the following PowerShell script that will parse some very large file for
I have the following tiny PowerShell script that's meant to kill some certain processes
I have the following code snippet from my PowerShell script that... Loops through a
I have the following powershell script which loads a custom .NET DLL this contains
I have created the following Powershell script which I hope to use to copy
I have the following log entry that I am processing in PowerShell I'm trying
My powershell script takes the following parameter: Param($BackedUpFilePath) The value that is getting passed
I have a PowerShell script that configures web site and web application settings in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.