0

The task seems trivial! A lot of users come and go in our environment. Every now and then I want to check for "orphan" roaming profile folders and get rid of them. My code works if the user name matches the folder name exactly but not for folders where Windows has added a .V2, .V3 etc. E.g. -eq works but not -like or -contains, or any of the ways I've tried to insert a wildcard. I tried a where-object construction and failed as well.

For example, if we have a user bob.smith, I want don't want my "orphan" list to include profile folders named bob.smith or bob.smith.V2.

Here's my code:

# Put profile folder names in array

$profilefolders = Get-ChildItem -Name "\\server\profiles$"



# Initialize arrays 

 $orphanprofiles = @()



foreach ($userprofile in $profilefolders)
    {
#  Do profile names match up with user accounts?
    If(Get-ADUser -Filter {SamAccountName -eq $userprofile})

        {
        # User account exists: no action
        }

    Else
        {
        # Profile is an orphan. Add to list.
        $orphanprofiles += $userprofile
                         }
                }




# put array values in a string variable with newline char for screen   output
$outputprofiles = $orphanprofiles -join "`n"

Write-Output "`n"
Write-Output "Orphan profiles: `n$outputprofiles"
Write-Output "  "
SKaye
  • 21
  • 5

2 Answers2

1

Change the first line to :

$profilefolders = Get-ChildItem -Name "\\server\profiles$"|where {$_ -notmach '.*\.V\d$'}

Came up with a better solution. It uses a RegEx to split the .V2 extension from the foldername and so checks the name without that ext. The intermediate $profilefolders isn't required.

# Initialize arrays 
$orphanprofiles = @()
$pattern = '^(.*?)(\.V\d)$'

Get-ChildItem -Name "\\server\profiles$"| 
    ForEach {
        If ($_ -match $pattern) {
            If (!(Get-ADUser -Filter {SamAccountName -eq $matches[1]}))
                {$orphanprofiles += $_} else {}
        } Else {
            If (!(Get-ADUser -Filter {SamAccountName -eq $_}))
                {$orphanprofiles += $_}
        }
    }


# put array values in a string variable with newline char for screen   output
$outputprofiles = $orphanprofiles -join "`n"
Write-Output "`n"
Write-Output "Orphan profiles: `n$outputprofiles"
Write-Output "  "
  • Awesome! First answer works, second generates a bunch of red, starting with the ! on line 8. There's a bunch of stuff in there I don't get so I'll go read up on it. Many thanks for the boost on the learning curve. – SKaye Dec 21 '16 at 14:55
  • Drat! #1 is not the answer: I don't want to ignore every folder with a .Vx appended, just ignore the .Vx part of it. – SKaye Dec 21 '16 at 15:39
  • @SKaye I changed the second script to have an additional pair of parentheses with the ` !` not operator. Haven't an AD at home, so can't test it. –  Dec 21 '16 at 16:07
  • Thanks LotPings, I also got rid of the red by adding double quotes, e.g. – SKaye Dec 21 '16 at 17:45
  • If ("! Get-ADUser -Filter {SamAccountName -eq $matches[1]}") – SKaye Dec 21 '16 at 17:46
  • However the $orphanprofiles is still getting populated with all folders, not just the nonmatching ones. – SKaye Dec 21 '16 at 17:47
  • @SKaye Might have to do with the stacked if's. Added an empty else in the above script. –  Dec 21 '16 at 17:52
  • I think so too, been trying to move around that final Else statement. Tried your mod, no change. (Incidentally, do you know how I can insert a linebreak in the comment? Been trying with no luck. This is a new account without edit rights.) – SKaye Dec 21 '16 at 17:56
  • Interesting! I stuck in some write-host statements to find out where the overpopulation is happening: it's all in the first loop, not the Else. I'll dig into that Regex some more. – SKaye Dec 21 '16 at 18:03
  • @SKaye Well I guess when an orphaned profile has .v2 or .v3 attached the script should add that to the var not the shortened version. I'll change the script. –  Dec 21 '16 at 18:08
  • Finally got this one! It was tricky. Many thanks, @LotPings, for the pointers - got a new technique in the bag now. It came down to two changes to the -Filter statement. I had to get rid of the ! (not) and go with a positive match, so the "If" action is empty and the "Else" contains the action (add this folder to the $orphanfolders array). Also, I couldn't use $matches[1] in the comparison operator with SamAccountName: I had to set a regular variable to contain that value above the If statement. Phew!! – SKaye Dec 23 '16 at 15:59
  • @SKaye If you think it's worth sharing - edit your question or add your own answer. –  Dec 23 '16 at 16:03
0

Many thanks to @LotPings. Here is my code now (with all the debug statements):

# Initialize array
$orphanprofiles = @()

# set regex pattern
$pattern = '^(.*?)(\.V\d)$'

Get-ChildItem -Name "\\server\profiles$"| 
    ForEach {
        Write-Host "Testfolder: " $_
        If ($_ -match $pattern) 
        {
          Write-Host "Loop 1 Foldername " $_
          Write-Host "Loop 1 Match " $matches[1]
          $Vnuser = $matches[1]

            If(Get-ADUser -Filter {SamAccountName -eq $Vnuser})
                {
             # match: do nothing
                }
               Else 
                {
                $orphanprofiles += $_
                Write-Host "Loop one inside If statement: " $_
                }


     }

      Else
        {
           Write-Host "Folders without .Vn: " $_
           If(Get-ADUser -Filter {SamAccountName -eq $_})
                {
                # match: do nothing
                }
            Else
                {
                $orphanprofiles += $_
                Write-Host "Loop 2 foldername: " $_
                }

   } 

   }



# put array values in a string variable with newline char for screen output
$outputprofiles = $orphanprofiles -join "`n"
Write-Output "`n"
Write-Output "Orphan profiles: `n$outputprofiles"
Write-Output "  "
SKaye
  • 21
  • 5