×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Comparing a CSV to a Share

Comparing a CSV to a Share

Comparing a CSV to a Share

(OP)
Good Day,

We have several thousand users in our Active Directory. I have created a PS script to pull all of the users into a CSV file.

In a share we have folder for each user - about two thousand of these folders are old and need to be removed.

I would like to compare my CSV file of active users against the list of folder names in the share, listing all of the folders that are old and need to be removed.

I'm about at my wits end trying to get my script to run properly.

Any tips on a proper way to script this would be great.

Kindest Regards,

Matt

RE: Comparing a CSV to a Share

Matt
Are you still in need of assistance? I've been away for a while, but I'd be willing to help out, now, if I can.

If so, what would determine if a file was "old and need to be removed"? They don't show as a user in your AD user export?

Assumptions I'm making until I hear from you:
1. Directory names are equal to the users' SamAccountName
2. You have a SamAccountName attribute in the AD export

So, I think it would go something like this

CODE -->

$import = Import-Csv "C:\temp\user_export.csv"
$users = @()
foreach ($i in $import)
	{
	 $users += $i.SamAccountName
	}

#get directories$path = "c:\temp\userdirs\"
$dirs = Get-ChildItem -Path $path -Name

#find directories to remove
$removals = @()
foreach ($dir in $dirs)
	{
	 #directory is not in the list of users
	 if (!($users -contains $dir))
		{
		 #add dir to removal list
		 $removals += $dir
		}
	}
$removals | Out-file "C:\temp\remove_dirs.txt" 

If you don't need to review the list you could remove them from the script instead of exporting them. You can add something like this into the if-statement:

CODE

if (!($users -contains $dir))
	{
	 #add dir to removal list
	 $removals += $dir
	 #remove directories
	 $dir2remove = $path+$dir
	 Remove-Item $dir2remove -Recurse -Force -Confirm:$False -EA SilentlyContinue
	} 


Light travels faster than sound. That's why some people appear bright until you hear them speak.

RE: Comparing a CSV to a Share

I see a typo in the first code block:

CODE

#get directories$path = "c:\temp\userdirs\" 

Should be:

CODE

#get directories
$path = "c:\temp\userdirs\" 


Light travels faster than sound. That's why some people appear bright until you hear them speak.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close