Quantcast
Channel: PowerShell.com – PowerShell Scripts, Tips, Forums, and Resources
Viewing all articles
Browse latest Browse all 6937

Deleting all children of home folders in a specific OU

$
0
0

Hi All,

 

I'm having an issue with my code where when I try to delete all the folders within the home directory but not the directory itself it will just delete my script rather than do what it's functioned to do. Here is the code:

#==============================================================================

# File:    cleanupHomeDirectoriestudents.ps1

# Author:  Jason Singh

# Date:    7/24/2014

# Purpose: Cleans up student home directories from a specified school.

#==============================================================================

 

#Imports AD module...

Import-Module ActiveDirectory

 

#Takes in a user inputted variable to determine which school students to clean up home directories for...

$school = Read-Host 'Which school needs student home directories cleaned up?(Please answer, PPS, UMS, MBS, CMS, or CHS.)'

 

#Uses and if else statement to determine which school after input...

If ($school -eq 'PPS')

{

$searchOU = 'Ou=Students,OU=PPS,OU=District,DC=csd,DC=local'

}

ElseIf ($school -eq 'UMS')

{

$searchOU = 'Ou=Students,OU=UMS,OU=District,DC=csd,DC=local'

}

ElseIf ($school -eq 'MBS')

{

$searchOU = 'Ou=Students,OU=MBS,OU=District,DC=csd,DC=local'

}

ElseIf ($school -eq 'CMS')

{

$searchOU = 'Ou=Students,OU=CMS,OU=District,DC=csd,DC=local'

}

ElseIf ($school -eq 'CHS')

{

$searchOU = 'Ou=Students,OU=CHS,OU=District,DC=csd,DC=local'

}

Else

{

Write-Host 'Not a valid answer. Please run the script again to continue...'

exit

}

 

#Sets a variable to users which is all users found in the specified OU and their home directories...

$users = Get-ADuser -filter * -SearchBase $searchOU -properties HomeDirectory

 

#Using a foreach loop it will look in each students home directory and delete everything except the home directory folder...

foreach ($user in $users) { 

Get-ChildItem $user.HomeDirectory -Force | Remove-Item -Force -Recurse

 

}

 

Write-Host 'Student file cleanup successful.'

 

 

 

I'm absolutely stumped by the result it does. I do know at one point it did actually cleanup a set of accounts on a test OU when it was in a test phase. Any thoughts or help on this would be great.

 


Viewing all articles
Browse latest Browse all 6937

Trending Articles