Quantcast
Channel: Windows PowerShell forum
Viewing all articles
Browse latest Browse all 21975

Active Directory Script-Find if users exist

$
0
0

Import-CSV "L:.\Users.txt" -header ("UserName") | % {
   $UserN = $_.UserName
   $ObjFilter = "(&(objectCategory=person)(objectCategory=User)(samaccountname=$UserN))"
   $User = Get-ADUser -Filter {sAMAccountName -eq $UserN}
 $objSearch = New-Object System.DirectoryServices.DirectorySearcher
 $objSearch.Filter = $ObjFilter 
 $objSearch.SearchRoot = "LDAP://ou=Remove this if you dont want only users in a OU returned,dc=Domain,dc=co,dc=uk"
 $AllObj = $objSearch.findOne()
 $user = [ADSI] $AllObj.path
 $ErrorActionPreference = "silentlycontinue"
 If ($User -eq $Null) {Write-host "Domain\$UserN does not exist in AD"}
 Else {Write-host "Domain\$UserN found in AD"}
 }

-Can anybody help me step by step. I just started using powershell today. I am trying to use powershell to check if users exist in AD. I manage to create a script to check for one user at a time but, I really what a script to check multiple users at the same time. This script is currently telling me everybody does not exist. Even if I put a user that does.




Viewing all articles
Browse latest Browse all 21975

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>