Solved

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

Posted on 2016-09-27
6
38 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

 
LVL 7

Author Comment

by:Senior IT System Engineer
Comment Utility
@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
Comment Utility
Change Get-ADUser $Folder.name

To

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

Author Closing Comment

by:Senior IT System Engineer
Comment Utility
Thanks Subsun !
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

I thought I'd write this up for anyone who has a request to create an anonymous whistle-blower-type submission form created using SharePoint 2010 (this would probably work the same for 2013). It's not 100% fool-proof but it's as close as you can get…
Create and license users in Office 365 in bulk based on a CSV file. A step-by-step guide with PowerShell script examples.
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…
This tutorial will walk an individual through the process of configuring their Windows Server 2012 domain controller to synchronize its time with a trusted, external resource. Use Google, Bing, or other preferred search engine to locate trusted NTP …

728 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now