• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 294
  • Last Modified:

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.
0
mishcondereya
Asked:
mishcondereya
  • 6
  • 5
1 Solution
 
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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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
 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 6
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now