Compare Windows Folder with CSV

Hello

I have a CSV spreadsheet which contains all our LIVE users within Active Directory. We appears to have a lot of Home Areas which have never been deleted after the user has left the firm.

How do I compare a specific Windows Folder (Share) with a CSV? From that, I can then delete the necessary folders from the shared location.

Are there any freebie apps that do this?

Thanks.
mishcondereyaAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Chris DentPowerShell DeveloperCommented:

PowerShell? :)

http://support.microsoft.com/kb/968929

Now it kind of depends on what you have, although whatever you have there will be a way.

When you say Share, do you mean \\Server\Share? Or do you mean a sub-folder of that? This will dictate how we go about comparing your list with things running on the server.

Imagine our CSV file looks like this:

User Name,SamAccountName
Chris Dent,chris

If you have sub-folders we might approach it like this:

$CSV = Import-CSV "YourCSVFile.csv"
Get-ChildItem "\\Server\Share" | %{
  $Name = $_.Name
  # Check to see if the folder name exists in the CSV
  If (($CSV | ?{ $_.SamAccountName -Like $Name }) -eq $Null) {
    # And if it does not, this folder can be removed / archived
    # Do something with the folder
  }
}

Or if you're looking at Shares, not much different from the first:

$CSV = Import-CSV "YourCSVFile.csv"
Get-WMIObject Win32_Share -Computer Server | %{
  $Name = $_.Name
  # Check to see if the folder name exists in the CSV
  If (($CSV | ?{ $_.SamAccountName -Like $Name }) -eq $Null) {
    # And if it does not, this folder can be removed / archived
    # Do something with the folder
  }
}

Chris
0
mishcondereyaAuthor Commented:
Hi Chris

I already have a CSV which contains info like this;

sAMAccountName
James.Bond

I can confirm the share is \\Server01\Home. So under Home there will be users home areas. Such as James.Bond, Indiana.Jones etc etc.

In which case, I would assume I run your second bit of code using PowerShell??? Correct.

Thanks.
0
Chris DentPowerShell DeveloperCommented:

The first, as below :)

What do you want to do when you find one that doesn't exist? I've told the script below to tell you about it, but if you want to archive it, rename it, delete it, compress it, etc, etc we can do that.

Chris
$CSV = Import-CSV "YourCSVFile.csv"

# Get the sub-folders of \\Server\Share
Get-ChildItem "\\Server\Share" | %{

  # Hold onto the folder Name
  $Name = $_.Name

  # Check to see if the folder name exists in the CSV
  If (($CSV | ?{ $_.SamAccountName -Like $Name }) -eq $Null) {
    # And if it does not, this folder can be removed / archived
    # Do something with the folder

    Write-Host "No user associated with $($_.FullName)" -ForegroundColor Yellow
  }
}

Open in new window

0
Protecting & Securing Your Critical Data

Considering 93 percent of companies file for bankruptcy within 12 months of a disaster that blocked access to their data for 10 days or more, planning for the worst is just smart business. Learn how Acronis Backup integrates security at every stage

mishcondereyaAuthor Commented:
Dude, that's an awesome piece of code.

How about move the folders to \\Server\Home\OLD. Only folders that DO NOT appear in the CSV. Is this doable?

Also, line 1. You've put in brackets YourCSVFile. Do I have to use the full path, eg C:\Users.CSV?
0
Chris DentPowerShell DeveloperCommented:

> Also, line 1. You've put in brackets YourCSVFile. Do I have to use the full path, eg C:\Users.CSV?

Either a full path or a relative path. In this example you run the code above from the same directory as the CSV file. If the file is elsewhere you should use a full path.

> How about move the folders to \\Server\Home\OLD. Only folders that DO NOT appear in the CSV. Is this doable?

Sure :)

There's a catch though, either we have to tell the script to avoid looking at OLD, or OLD has to be part of a different directory structure. This version avoids OLD but I would put OLD elsewhere personally.

Make absolutely sure it doesn't return anything you don't want to move before you let it start moving things around. To help with that I've added -WhatIf, tells you what it's going to do without actually doing it.

Chris
$ArchivePath = "\\Server\Home\OLD"
$CSV = Import-CSV "YourCSVFile.csv"

# Get the sub-folders of \\Server\Share
Get-ChildItem "\\Server\Share" | ?{ $_.Name -NotLike "OLD" } | %{

  # Hold onto the folder Name
  $Name = $_.Name

  # Check to see if the folder name exists in the CSV
  If (($CSV | ?{ $_.SamAccountName -Like $Name }) -eq $Null) {
    # And if it does not, this folder can be removed / archived
    # Do something with the folder

    Write-Host "No user associated with $($_.FullName)" -ForegroundColor Yellow

    # Move the folder - Note -WhatIf is set, the script will only say what it is going to do.

    Move-Item $_.FullName -Destination $ArchivePath -WhatIf
  }
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mishcondereyaAuthor Commented:
Hi

I ran the above code, but can confirm none of the home areas moved to the OLD folder.

For example, this is a message PowerShell returned.

No user associated with \\server\home\james.bond
What if: Performing operation "Move Directory" on Target "Item: \\server\home\james.bond Destination: \\server\Home\OLD\james.bond".

It just then continued to the next user folder straight away. I can confirm I can open the folder, and that Domain Admins have full access.

Any ideas?

Thanks.
0
Chris DentPowerShell DeveloperCommented:

> I ran the above code, but can confirm none of the home areas moved to the OLD folder.

That's because of "-WhatIf". If you're happy it's returning the right folders remove -WhatIf and it'll actually start doing things. I added it, as I noted above, so you could test the script without risking it doing too much.

Chris
0
mishcondereyaAuthor Commented:
Apologies, I must of skimmed that part.

I shall try the code without the 'WhatIf'.
0
Chris DentPowerShell DeveloperCommented:

Cool, I'll cross my fingers :)

Chris
0
mishcondereyaAuthor Commented:
Worked like a charm!!
0
mishcondereyaAuthor Commented:
Top man!!!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Windows Server 2003

From novice to tech pro — start learning today.