Question

Trying to determine if there are any user folders on the network that don’t have an associated user account. All results return "Missing" when the majority should return "Found". Any ideas?

$Dir = "\\ServerName\Share\"
$FolderList = Get-ChildItem($Dir) | where {$_.psIsContainer -eq $true}
$UserList = get-qaduser -sizelimit 0 | select LogonName

foreach ($Folder in $FolderList)
{
if ($UserList -contains $Folder.name)
{
"Found:  " + $Folder.name
}
Else
{
"Missing:  " + $Folder.name
}
}
Was it helpful?

Solution

How about trying a slightly different approach that uses a hashtable (which offers exceptionally fast lookups of keys):

$users = @{}
Get-QADUser -sizelimit 0 | Foreach {$users["$($_.LogonName)"] = $true}
$dir = "\\ServerName\Share\"
Get-ChildItem $dir | Where {$_.PSIsContainer -and !$users["$($_.Name)"]}

If the folder name doesn't exactly match the LogonName, then as EBGreen notes, you will need to adjust the key ($users["$($.LogonName)"]) or the folder name when you use it to index the hashtable (!$users["$($.Name)"]).

OTHER TIPS

-contains will match if the item in the collection is identical to what you are testing so be sure that the $Folder.Name is exactly the same as LogonName. Usually it wouldn't be. Most companies would have the folder name be foo$ for a user named foo.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top