Scenario: We have roaming profiles at work. During our upgrade of client machines from XP to Win7, the Win7.v2 subfolder is not being created with the inheritance flag set (minor problem easily fixed). On top of this, we also have a plethora of old profiles (winxp, winxp.old, winxp_old, winxp_, win7.v2_old etc) that need to be purged. I have come up with a script to attempt to do this but am stuck on the deletion of old profiles.
Environment: Roaming profiles are in the following format:
- P:\Profiles$\User1\WIN7.V2
- P:\Profiles$\User1\winxp
- P:\Proifles$\User1\WIN7.V2_old
- ...
I am self taught so apologies for rubbish scripting. I haven't used the $date variable yet but will look to not delete any folder that was modified in the last 10 days.
cls
# Date and time script is started
$StartDate = date
# Date variable for 30 day buffer
$date = (Get-Date).AddDays(-30)
# Sets path and log variables
$ProfilePath = "D:\Work\Profiles"
$LogPath = "D:\Work\Logs"
$Takeownlog = "$LogPath\Takeown.log"
$Icaclslog = "$LogPath\icacls.log"
$NoWIN7FolderLog = "$LogPath\NoWin7Folder.log"
# Deletes any previous log entries
del $Takeownlog
del $Icaclslog
del $NoWIN7FolderLog
# Gets Subfolder list
$FolderList = Get-ChildItem $ProfilePath
# Main body of script.
foreach ($SubFolder in $FolderList)
{
$winxp = "$ProfilePath\$subfolder\winxp"
$winos = "$ProfilePath\$subfolder\%winos%"
$winvar = "$ProfilePath\$subfolder\win"
# Checks if the WIN7.V2 folder exists. If it doesn't, it logs it and moves to next folder
if(-not(Test-Path -path $ProfilePath\$SubFolder\WIN7.V2)){
Write-Host "$SubFolder\WIN7.V2 does not exist. Moving on..." -ForegroundColor Red
Write-Output "$ProfilePath\$SubFolder\WIN7.V2 does not exist" | Out-File $NoWIN7FolderLog -Append -encoding default
} Else
{
# If the WIN7.V2 folder does exist it will recursively set Ownership to Administrators and then set the inheritance on the WIN7.V2 folder
Write-Host "Fixing ownership and inheritance: $ProfilePath\$SubFolder" -foregroundcolor Green
Write-Output "Fixing ownership and ineritance: $ProfilePath\$SubFolder\WIN7.V2" | Out-File $Takeownlog -append -encoding Default
takeown /f $ProfilePath\$SubFolder\WIN7.V2 /A /R /D Y | Out-File $Takeownlog -append -encoding Default
Write-Output "" | Out-File $Takeownlog -append -encoding Default
#
Write-Output "" | Out-File $Icaclslog -append -encoding Default
Write-Output "Fixing inheritance: $ProfilePath\$SubFolder\WIN7.V2" | Out-File $Icaclslog -append -encoding Default
ICACLS $ProfilePath\$SubFolder\WIN7.V2 /inheritance:e /c /t | Out-File $Icaclslog -append -encoding Default
}
# Deletes any old profiles winxp or win7.v2_*
Write-Host "Removing any old profiles..."
if(Test-Path -path $winxp){
#if((-not(Test-Path -Path $winxp)) -and (-not(Test-Path -Path $winos)) -and (-not(Test-Path -Path $winvar)) {
write-host "No old profiles to delete for $SubFolder"
} Else
{
# If any old profiles are found it will delete them
Write-Host "Old profiles found for $subfolder. Deleting now..."
Remove-Item -Path $winxp
}
}
Write-Host ""
$EndDate = date
Write-Host "Started: $StartDate"
Write-Host "Ended: $EndDate"
Write-Host ""
The first part of the script to reset inheritance works fine as below:
Fixing ownership and inheritance: D:\Work\Profiles\mcbridt
Fixing ownership and inheritance: D:\Work\Profiles\singhj
Fixing ownership and inheritance: D:\Work\Profiles\test1
test2\WIN7.V2 does not exist. Moving on...
Started: 04/13/2015 16:25:09
Ended: 04/13/2015 16:25:09
But the second part to delete any 'old' profiles does not work at all. I have tried many iterations of remove-item but cannot for the life of me figure it out. I appreciate any suggestions and fixes.
Many thanks