Configurar powershell y script para borrar los ficheros más antiguos de 15 días

Configurar powershell y script para borrar los ficheros más antiguos de 15 días

Ejecutamos:  powershell como administrador

Miramos como está configurado: Get-ExecutionPolicy

Ejecutamos: Set-ExecutionPolicy RemoteSigned

Creamos un fichero.ps1 y editamos:

$limit = (Get-Date).AddDays(-15)
$path = «C:\directorio»

# 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

 

Enlaces de interes:

PowerShell: delete files older than x days

 

 

1 comentario en “Configurar powershell y script para borrar los ficheros más antiguos de 15 días”

  1. Hola amigo gracias por el aporte, he probado el script

    lo deje asi

    Set-ExecutionPolicy RemoteSigned

    $limit = (Get-Date).AddDays(-2)
    $path = «\\192.168.X.XXX\Respaldos\BK_SAP»
    # 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

    sin embargo al ejecutarlo no me realiza la tarea, podrias hecharme una mano, estoy iniciando en esto de powershell

     

Los comentarios están cerrados.

Tu ventana semanal al mundo de la informática

Noticias, consejos y tendencias tecnológicas para mantenerte actualizado de forma sencilla.