This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
script:powershell:delete [2020/06/28 09:56] 127.0.0.1 external edit |
script:powershell:delete [2020/08/10 23:07] (current) |
||
---|---|---|---|
Line 17: | Line 17: | ||
__Supprimer un seul dossier__ | __Supprimer un seul dossier__ | ||
- | <file> | + | <sxh> |
remove-item "Chemin du dossier complet" -Force -erroraction SilentlyContinue | remove-item "Chemin du dossier complet" -Force -erroraction SilentlyContinue | ||
- | </file> | + | </sxh> |
__Supprimer plusieurs éléments avec un filtre__ | __Supprimer plusieurs éléments avec un filtre__ | ||
Line 27: | Line 27: | ||
__Supprimer une clef de registre__ | __Supprimer une clef de registre__ | ||
- | <file> | + | <sxh> |
remove-item hklm:\software\demo -recurse | remove-item hklm:\software\demo -recurse | ||
- | </file> | + | </sxh> |
+ | |||
+ | __Supprimer un objet plus vieux que 30 jours__ | ||
+ | |||
+ | <sxh> | ||
+ | # Make a limite rule | ||
+ | $limit = (Get-Date).AddDays(-30) | ||
+ | |||
+ | # Set the path | ||
+ | $path = "C:\Some\Path" | ||
+ | |||
+ | # Delete files older than the $limit. | ||
+ | Get-ChildItem -Path $path -Recurse -Force | Where-Object { !$_.PSIsContainer -and $_.CreationTime -lt $limit } | Remove-Item -Force | ||
+ | |||
+ | # Delete any empty directories left behind after deleting the old files. | ||
+ | Get-ChildItem -Path $path -Recurse -Force | Where-Object { $_.PSIsContainer -and (Get-ChildItem -Path $_.FullName -Recurse -Force | Where-Object { !$_.PSIsContainer }) -eq $null } | Remove-Item -Force -Recurse | ||
+ | </sxh> |