Rename folders in file share

1.9k Views Asked by At

The script is comparing folder names in a share where we store user profiles to those in AD and seeing which ones are disabled or don't exist and counting them. I now want to take that list and rename all the user folders that aren't in AD to .old.

$delusercount = 0
$usernotfoundcount = 0
$foldernames = (Get-ChildItem \\kiewitplaza\vdi\appsense_profiles).Name

foreach($name in $foldernames)
{
    try
    {
       $user = get-aduser $name -properties enabled
       if($user.enabled -eq $false)
       {
          $delusercount = $delusercount + 1
       }
    }
    catch
    {
        $usernotfoundcount = $usernotfoundcount + 1
    }
}  

write-host "User disabled in AD count " $delusercount
write-host "User ID NotFound in AD count " $usernotfoundcount 
1

There are 1 best solutions below

0
Kev On BEST ANSWER

How about just after:

$delusercount = $delusercount + 1

Insert:

rename-item "\\kiewitplaza\vdi\appsense_profiles\$name" `
            "\\kiewitplaza\vdi\appsense_profiles\$name.old"

Update:

You probably also want to insert just after your foreach statement:

if($name.EndsWith(".old"))
{
    continue
}

This will prevent previously renamed folders from being processed and renamed again thus ending up for example with the folder for user Bob becoming Bob.old then potentially Bob.old.old then Bob.old.old.old and so on.