I'm trying to determine which user folders in C:\Users have active users in Active Directory.
I currently have the following:
$userProfile = Get-ChildItem -Path "C:\Users"
$unknownList = @()
foreach($user in $userProfile){
try{
Get-ADUser -Identity $user.Name | Out-Null
}
catch{
$unknownList += $user.Name
}
}
Write-Host $unknownList
My issue is that all usernames appear to not exist and are caught. Can anyone offer some suggestions for a PowerShell first-timer? I have a tried a number of other things found here and elsewhere but none have been able to work. Thank you!
C:\Usersdoes not nesessarly match the name of the user in AD. It could beusername.DomainName.###if the user profile already existed.Catch { }block might never be reached, because Get-ADUser creates non-terminating errors. To work around this, use-ErrorAction StoponGet-ADUser. That changes errors into terminating errors.$unknownList.$unknownList? Add a lineWrite-Error $_to thecatchblock so you can see what's going on. Are you using PowerShell v2? PowerShell didn't support automatic module loading before v3, so you'd need to runImport-Module ActiveDirectorybefore you could useGet-ADUser. You can check your PowerShell version by echoing$PSVersionTable.