Can't delete a folder

Hello powershell community =)

 

I’m having troubles with a script i’m working on to “clean” computers.

I’m working on a function that scans profiles folders in “C:\users” and only propose to delete the ones with a “lastwritetime” older than 9 months.

Everything works fine only for the “remove” part.

When i try to delete the folder, i get a message that the path is too long. i think that the problem comes from the fact that my variable has too much information, but i can’t find a way to only point to the folder i want to delete.

here is my code:

[pre]

cd c:\users
$datemax = (Get-Date).adddays(-274)
$profils = Get-ChildItem C:\users
$profils
Write-host “`nVoici la liste des profils présent sur le poste” -BackgroundColor Black -ForegroundColor Yellow
Write-host “En appuyant sur entrée le script va vous proposer de supprimer certains profils” -BackgroundColor Black -ForegroundColor Yellow
Write-host “les profils selectionné par le script sont tous non utilisés depuis plus de 9 mois” -BackgroundColor Black -ForegroundColor Yellow
Pause
Foreach ($item in $profils){
$assez = $item.lastwritetime -lt $datemax
if ($assez -eq “true”) {
$itemtime = $item.lastwritetime
$itempath = resolve-path $item
write-host “`nvous allez supprimer le dossier $item” -BackgroundColor black -ForegroundColor yellow
write-host “Le profil n’a pas était utilisé depuis $itemtime et dépasse donc la limite des 9 mois d’inactivités” -BackgroundColor black -ForegroundColor yellow
write-host " "
$confirmation = Read-Host “Etes vous sur de vouloir continuer? merci de taper ‘OUI’”
if ($confirmation -eq ‘oui’){
takeown /f $itempath /a /r
remove-item $itempath -force -Recurse
pause
}
}
else {
write-host “`nXXXXXXX suppression annulé XXXXXXX” -backgroundcolor Black -ForegroundColor red{
}
}
}
}

[/pre]

 

 

here is the message i get when i try to delete:

remove-item : impossible to find parts of the path ‘C:\users\8003109.old\AppData\Local\Application Data’.
Au caractère C:\temp\Script\Espace disque\Espace disque.ps1:139 : 13

  • remove-item $fichier -force -Recurse
  • CategoryInfo : WriteError: (C:\users\8003109.old:String) [Remove-Item], DirectoryNotFoundException
  • FullyQualifiedErrorId : RemoveItemIOError,Microsoft.PowerShell.Commands.RemoveItemCommand

I’m trying to delete c:\users\8003109.old and it points to C:\users\8003109.old\AppData\Local\Application Data

 

i also tried:

 

[pre]

cd c:\users
$datemax = (Get-Date).adddays(-274)
$profils = Get-ChildItem C:\users
$profils
Write-host “`nVoici la liste des profils présent sur le poste” -BackgroundColor Black -ForegroundColor Yellow
Write-host “En appuyant sur entrée le script va vous proposer de supprimer certains profils” -BackgroundColor Black -ForegroundColor Yellow
Write-host “les profils selectionné par le script sont tous non utilisés depuis plus de 9 mois” -BackgroundColor Black -ForegroundColor Yellow
Pause
Foreach ($item in $profils){
$assez = $item.lastwritetime -lt $datemax
if ($assez -eq “true”) {
$itemtime = $item.lastwritetime
$itempath = resolve-path $item
write-host “`nvous allez supprimer le dossier $item” -BackgroundColor black -ForegroundColor yellow
write-host “Le profil n’a pas était utilisé depuis $itemtime et dépasse donc la limite des 9 mois d’inactivités” -BackgroundColor black -ForegroundColor yellow
write-host " "
$confirmation = Read-Host “Etes vous sur de vouloir continuer? merci de taper ‘OUI’”
if ($confirmation -eq ‘oui’){
takeown /f $item /a /r

foreach ($file in $item){
remove-item $file -force -Recurse
pause
}
}

}
else {
write-host “`nXXXXXXX suppression annulé XXXXXXX” -backgroundcolor Black -ForegroundColor red{
}
}
}
}

[/pre]

On old trick to delete unwilling files or folders is to use robocopy with the option /MIR to copy an emtpy folder to the target folder to get rid of it. :wink: It should work for deletion of too long paths as well.

Hi, thanks a lot for the tip :slight_smile:
Can you show me how i would do that? i’ve never really played with CMD .

am i doing this right?

[pre]

if ($confirmation -eq ‘oui’){
robocopy /mir $itempath
remove-item $itempath -force -Recurse
pause

[\pre]

You need an empty folder and then you “copy” this empty folder to the target folder you want have empty. If robocopy did its job you can delete the empty folder and the - now - empty target folder.

$TempFolder = Join-Path -Path $PWD -ChildPath New-Guid
If (-not (Test-Path -Path $TempFolder) ) {
New-Item -Path $TempFolder -ItemType Directory -Force | Out-Null
}
if ($confirmation -eq ‘oui’) {
robocopy $TempFolder $itempath /mir
}
Remove-Item -Path $TempFolder -Force
Remove-Item -Path $itempath -Force

In this example I use the current working directory to create an empty folder.

It works perfectly! i tweaked it just a little bit and its perfect! thanks a lot Olaf!