Albert Widjaja
asked on
Modifying Powershell script to list all Home drive or directories with no AD account associated.
Hi people,
I'd like to know if it is possible to modify the script below which search and list any User home flder which is still exist in the File Server but there is no owner or no AD account owning it:
Thanks in advance,
This is the series of question that I ask from: https://www.experts-exchange.com/questions/28972587/Modifying-PowerShell-to-list-home-drives-of-disabled-users.html
I'd like to know if it is possible to modify the script below which search and list any User home flder which is still exist in the File Server but there is no owner or no AD account owning it:
Get-ADUser -SearchBase "OU=Disabled USERS,OU=Main Office,DC=domain,DC=com" -F * -Pr HomeDirectory | ?{$_.HomeDirectory -match "^\\\\FileServerHQ\\Users"} | Select Name,HomeDirectory,@{N="Size";E={"{0:N2}" -f ((Get-ChildItem $_.HomeDirectory -Recurse | Measure-Object -property length -sum).sum / 1MB) + " MB"}} | Export-CSV C:\TEMP\report.csv -NoTypeInformation
Thanks in advance,
This is the series of question that I ask from: https://www.experts-exchange.com/questions/28972587/Modifying-PowerShell-to-list-home-drives-of-disabled-users.html
ASKER
Hi Subsun,
It returns back with some error:
This is the script that I executed:
It returns back with some error:
Select : The "E" key has a type, System.Object[], that is not valid; expected types are {System.String, System.Management.Automation.ScriptBlock}.
At line:3 char:109
+ ... n.Message | Select @{N="User";E={$Folder.Name}},@{N="HomeDirectory";E ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Select-Object], NotSupportedException
+ FullyQualifiedErrorId : DictionaryKeyIllegalValue1,Microsoft.PowerShell.Commands.SelectObjectCommand
This is the script that I executed:
GCI "\\FileServer06\Users" -Directory | %{$Folder = $_;Try {Get-ADUser $Folder.name}Catch{$_.Exception.Message | Select @{N="User";E={$Folder.Name}},@{N="HomeDirectory";E={$Folder.Fullname}}, @{N="Size";E={"{0:N2}" -f ((Get-ChildItem $_.HomeDirectory -Recurse | Measure-Object -property length -sum).sum / 1MB) + " MB"} ,@{N="Error";E={$_}}}}} | Export-CSV C:\TEMP\reportOrphanedDir.csv -NoTypeInformation
Try..
GCI "\\FileServer06\Users" -Directory | %{$Folder = $_;Try {Get-ADUser $Folder.name}Catch{$_.Exception.Message | Select @{N="User";E={$Folder.Name}},@{N="HomeDirectory";E={$Folder.Fullname}}, @{N="Size";E={"{0:N2}" -f ((Get-ChildItem $Folder.Fullname -Recurse | Measure-Object -property length -sum).sum / 1MB) + " MB"}} ,@{N="Error";E={$_}}}} | Export-CSV C:\TEMP\reportOrphanedDir.csv -NoTypeInformation
ASKER
@Subsun,
Thanks for the reply.
However, the result is the full dump of my AD account:
Thanks for the reply.
However, the result is the full dump of my AD account:
DistinguishedName Enabled GivenName Name ObjectClass ObjectGUID SamAccountName SID Surname UserPrincipalName
so not the orphaned directory with the size ?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks Subsun !
Open in new window
OrOpen in new window
Script searches through the home folders inside \\FileServerHQ\Users and lists the folders which it fails to find a user in AD.