Solved

Modifying PowerShell to list home drives of disabled users ?

Posted on 2016-09-27
6
47 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
  • 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 7

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
 
LVL 7

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 7

Author Comment

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

Author Closing Comment

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

Join & Write a Comment

Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
Disabling the Directory Sync Service Account in Office 365 will stop directory synchronization from working.
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
The viewer will learn how to count occurrences of each item in an array.

760 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

17 Experts available now in Live!

Get 1:1 Help Now