Link to home
Start Free TrialLog in
Avatar of Albert Widjaja
Albert WidjajaFlag for Australia

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:

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

Open in new window


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
Avatar of SubSun
SubSun
Flag of India image

Try..
GCI "\\FileServerHQ\Users" -Directory | %{$Folder = $_;Try {Get-ADUser $Folder.name}Catch{$_.Exception.Message | Select @{N="User";E={$Folder.Name}},@{N="HomeDirectory";E={$Folder.Fullname}},@{N="Error";E={$_}}}} | Export-CSV C:\TEMP\report.csv -NTI

Open in new window

Or
GCI "\\FileServerHQ\Users" | ?{ $_.PSIsContainer } | %{$Folder = $_;Try {Get-ADUser $Folder.name}Catch{$_.Exception.Message | Select @{N="User";E={$Folder.Name}},@{N="HomeDirectory";E={$Folder.Fullname}},@{N="Error";E={$_}}}} | Export-CSV C:\TEMP\report.csv -NTI

Open 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.
Avatar of Albert Widjaja

ASKER

Hi Subsun,

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

Open in new window


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

Open in new window

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

Open in new window

@Subsun,

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

Open in new window

so not the orphaned directory with the size ?
ASKER CERTIFIED SOLUTION
Avatar of SubSun
SubSun
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks Subsun !