Powershell – Howto remove files and folder that is older than X days

Hello script geeks!

Today I want to talk about how you can easiest remove files and folder that is older than X days. You can get use of cmdlet Get-ChildItem to List all files and folder, where CreationDate i older than X days. And to remove the items get use of cmdlet Remove-Item.

Run following cmdlet:

$FilePath = "<-Path-> Ex: c:\test\" # Set Folder to check files and folder
$FileThatIsOlderThanDays = -7 # Set number of days back in time you will go

Get-ChildItem -Path ($FilePath) -Recurse | Where-Object {$_.CreationDate -le (Get-Date).AddDays($FileThatIsOlderThanDays)} | Remove-Item -Force

 

Lämna ett svar

E-postadressen publiceras inte. Obligatoriska fält är märkta *

Howto – Enable Powershell Remoteing in Windows Domain

Here comes some instructions, howto Enable Powershell Remoting in a Windows Domain. Step 1 – Set Powershell Execute Policy: By Default the powershell allow only code-signed powershell script. To be

Howto Force remote clients to update their Sccm clients Machine Policy Retrieval & Evaluation Cycle via Powershell

Hello! Here is some script to force a Sccm Client’s Machine Policy Cycle on remote client in a domain. But first. You need to be sure you have enable the

Howto – Connect to Configuration Manager 2012 from Powershell

Hi, Emil here. Have you ever wonder howto connect to a configuration manager 2012 site from Powershell? Okay, follow the steps below to success: Requirements: SCCM 2012 Console Powershell 3.0