Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stop printing amount of the element in foreach loop powershell

The foreach loop just keeps print out the amount of element 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
like image 823
Ender Avatar asked Jan 03 '23 20:01

Ender


2 Answers

Change this:

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

To:

foreach($pro in $properies)
{
    $ADSearch.PropertiesToLoad.add($pro)| out-null
}
like image 50
Ranadip Dutta Avatar answered Jan 06 '23 08:01

Ranadip Dutta


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

foreach($pro in $properies)
{
    [void]$ADSearch.PropertiesToLoad.add($pro)
}
like image 39
Raf Avatar answered Jan 06 '23 08:01

Raf