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]