Solved

Modifying Powershell script to list all Home drive or directories with no AD account associated.

Posted on 2016-09-27
6
96 Views
Last Modified: 2016-09-29
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
0
Comment
  • 3
  • 3
6 Comments
 
LVL 40

Expert Comment

by:Subsun
ID: 41819121
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.
1
 
LVL 7

Author Comment

by:Senior IT System Engineer
ID: 41820834
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

0
 
LVL 40

Expert Comment

by:Subsun
ID: 41821025
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

0
VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

 
LVL 7

Author Comment

by:Senior IT System Engineer
ID: 41821062
@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 ?
0
 
LVL 40

Accepted Solution

by:
Subsun earned 500 total points
ID: 41821208
Change Get-ADUser $Folder.name

To

Get-ADUser $Folder.name | Out-null
1
 
LVL 7

Author Closing Comment

by:Senior IT System Engineer
ID: 41821222
Thanks Subsun !
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Windows 10 came with  a lot of built in applications, Some organisations leave them there, some will control them using GPO's. This Article is useful for those who do not want to have any applications in their image (example:me).
This article explains the steps required to use the default Photos screensaver to display branding/corporate images
This tutorial will walk an individual through the process of transferring the five major, necessary Active Directory Roles, commonly referred to as the FSMO roles from a Windows Server 2008 domain controller to a Windows Server 2012 domain controlle…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question