Solved

Modifying PowerShell to list home drives of disabled users ?

Posted on 2016-09-27
6
85 Views
Last Modified: 2016-09-27
Hi All,

Can anyone here please help me to modify the script below:

Get-Content D:\Temp\DisabledAccounts.txt | % {If ($Path = Get-Item "\\<Server>\<Share>\$($_)" -ea si) {Rename-Item -Path $Path.FullName -NewName "_Old_$($Path.Name)" -WhatIf}} 

Open in new window


So that it can go through AD OU and then list the directory to be deleted in the separate file server ?

Disabled OU location: OU=Disabled USERS,OU=Main Office,DC=domain,DC=com
File Server Location: \\FileServerHQ\Users


Thanks in advance.

Note:
I've found it from: https://www.experts-exchange.com/questions/28963489/Looking-for-a-way-to-find-if-the-AD-disabled-accounts-have-HOME-folders-in-a-specific-CIFS-share.html
0
Comment
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
6 Comments
 
LVL 40

Accepted Solution

by:
Subsun earned 500 total points
ID: 41817693
To get all users with HomeDirectory from a OU
Get-ADUser -SearchBase "OU=Disabled USERS,OU=Main Office,DC=domain,DC=com" -F * -Pr HomeDirectory | ?{$_.HomeDirectory} | Select Name,HomeDirectory | Export-CSV C:\report.csv -nti

Open in new window

To get all users with HomeDirectory on \\FileServerHQ\Users from a OU
Get-ADUser -SearchBase "OU=Disabled USERS,OU=Main Office,DC=domain,DC=com" -F * -Pr HomeDirectory | ?{$_.HomeDirectory -match "^\\\\FileServerHQ\\Users"} | Select Name,HomeDirectory | Export-CSV C:\report.csv -nti

Open in new window

1
 
LVL 8

Author Comment

by:Senior IT System Engineer
ID: 41817743
Subsun,

Many thanks for the code, so is it possible to get the "orphaned" directory as well where the AD account is already deleted ?
0
 
LVL 40

Expert Comment

by:Subsun
ID: 41817798
Yes, If your how folder share is in format \\Server\Users\<User sAMAccountName >

you can get the user sAMAccountName from the home folder path and check if user exist or not. If the user account doesn't exist in AD then the folder can be considered as orphaned.

For this you need to read all home folders and run the query against each home folder, that should be a separate script..
1
Turn Insights into Action

Communication across every corner of your business is essential to increase the velocity of your application delivery and support pipeline. Automate, standardize, and contextualize your communication processes with xMatters.

 
LVL 8

Author Comment

by:Senior IT System Engineer
ID: 41818879
Yes, the format is \\Server\Users\<User sAMAccountName >

I will post separate thread for this request.
0
 
LVL 8

Author Comment

by:Senior IT System Engineer
ID: 41819014
0
 
LVL 8

Author Closing Comment

by:Senior IT System Engineer
ID: 41819015
Thanks !
0

Featured Post

Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

Question has a verified solution.

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

Active Directory security has been a hot topic of late, and for good reason. With 90% of the world’s organization using this system to manage access to all parts of their IT infrastructure, knowing how to protect against threats and keep vulnerabil…
Here's a look at newsworthy articles and community happenings during the last month.
The viewer will learn how to dynamically set the form action using jQuery.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

690 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