Ender
Ender

Reputation: 845

Stop printing amount of the element in foreach loop powershell

The foreach loop just keeps print out the number of elements in it as the following code. I want to stop it from printing it out.

$ADSearch = New-Object System.DirectoryServices.DirectorySearcher
$ADSearch.SearchRoot ="LDAP://$Domain"
$ADSearch.SearchScope = "subtree"
$ADSearch.PageSize = 100
$ADSearch.Filter = "(objectClass=$objectClass)"

$properies =@("distinguishedName",
"sAMAccountName",
"mail",
"lastLogonTimeStamp",
"pwdLastSet",
"accountExpires",
"userAccountControl")

foreach($pro in $properies)
{
    $ADSearch.PropertiesToLoad.add($pro)   
}

At the moment it gives:

0
1
2
3
4
5
6

Upvotes: 5

Views: 1368

Answers (2)

Raf
Raf

Reputation: 10107

Another solution is to [void] the "offending" line:

foreach($pro in $properies)
{
    [void]$ADSearch.PropertiesToLoad.add($pro)
}

Upvotes: 4

Ranadip Dutta
Ranadip Dutta

Reputation: 9133

Change this:

foreach($pro in $properies)
{
    $ADSearch.PropertiesToLoad.add($pro)   
}

To:

foreach($pro in $properies)
{
    $ADSearch.PropertiesToLoad.add($pro)| out-null
}

Upvotes: 5

Related Questions