Any Powershell gurus?

shaitan667

n00b
Joined
Aug 31, 2012
Messages
10
Hi All,

I pretty much have no experience with powershell, but think it is going to be my easiest option to acheive the following. I have a CSV containing folder names (that correlate to AD login names) and this is an outline of what I need to do -

Need to verify that names in the CSV exist within AD
If user doesn't exist, then need to move folder from \\server\home$\ to a staging folder
Output results to CSV

Basically, we have an account deletion script, but it only deletes the contents of the users home folder, instead of the the actual folder, so we now have alot of empty folders :(

Is anyone able to assist me with this? Or already have something like this in existence that they wouldn't mind sharing?

Thanks
 
could you give us an example how those folder names correlate to the login names. Might need to do some reformatting.

also how is the data formatted in the csv file? Just a column of folder names?
 
That should be pretty easy to do --- I don't have time to put a bunch of code together, but I can point you in the right direction.

1. You'll need to load the activeDirectory module to work with the AD accounts.
Code:
import-module activeDirectory

2. You'll need to pull your CSV into powershell
Code:
$csv = import-csv .\mycsvfile.csv

3. You'll need to create a loop to go through all your users...
Code:
foreach ($user in $csv)
{
  # create a variable to hold the users homedir path
  # you can form it however you need to.
    $userDir = "\\server\share\" + $csv.username

  # Test to see if the user is in active directory or if it's been deleted...
  $userObject = $null
  $userObject = get-adobject $user.username

  If ($userObject -eq $null) 
    { 
        remote-item $userDir
        $userDir | out-file -filepath c:\my\logfile.log -append
   }        


}

The important part is to note that I refer to the 'username'. This is a field from the CSV file that is obtained from the CSV header. You can use whatever you want -- just has to match the header of your CSV. I've attached a sample CSV below that should show you want I mean.



Sample CSV file
Code:
Fist, last, username
Some, one, someone
Any, one, anyone


hrm... guess I did have time for some code. :)
 
Back
Top