1

I have this script which tries to delete all the folders that are older than 7 days. All folders are located under a specific directory called "BackupPath"

This is the script:

 $date=Get-Date -UFormat "%d-%m-%y"
 $BackupPathday="C:\"+$env:computername+"GPOBackup\$date"
 $BackupPath="C:\"+$env:computername+"GPOBackup"

 if ((Test-Path $BackupPathday) -eq 0) {
 New-Item -ItemType Directory -Force -Path $BackupPathday
 }
 else {
 Write-Host "Today´s backup already exists"
 }

 $Folders=Get-ChildItem $BackupPath

 foreach ($i in $Folders) {
  $Days=((Get-Date) - $i.CreationTime).Days
  #PSISContainer is true means that $i is a folder, ohterwise is a file
  if ($Days -ge 7 -and $i.PsISContainer -eq $True) {       
   $i.Delete() 
  }
 }

When I run it, I get this error message:

Exception calling "Delete" with "0" argument(s): "The directory is not empty. " At C:\Users\x\Desktop\power.ps1:18 char:14 + $i.Delete <<<< () + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : DotNetMethodException Exception calling "Delete" with "0" argument(s): "The directory is not empty. " At C:\Users\x\Desktop\power.ps1:18 char:14 + $i.Delete <<<< () + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : DotNetMethodException

Is there any way of force deleting these folders and its content? I don´t know if there is an existing method to do this as I´m new with PowerShell.

Thanks

3
  • I thought of that before too and It doesn´t work: Remove-Item : Cannot find path 'C:\Users\x\Desktop\26-09-15' because it does not exist... Commented Oct 4, 2015 at 22:23
  • Unfortunately it doesn´t work either. Remove-Item : Cannot bind argument to parameter 'Path' because it is null. Commented Oct 4, 2015 at 22:47
  • You might have to use GCI $path -Recurse | Remove-Item Commented Oct 5, 2015 at 0:21

1 Answer 1

1

The -Directory switch gets just the folders then Where-object filters those folders based on the date criteria and finally remove-item removes them.(Remove Whatif to apply the command)

Get-ChildItem -Path $BackupPath -Directory | 
   Where-Object { ((get-date) - $_.CreationTime).days -ge 7} | 
         Remove-Item -Recurse -WhatIf

Also When testing for a non-existing directory use

if( -not (Test-path c:\temp) ) {"Do something"}else { "nothing"}

means if the expression evaluates to false then "Do something" else "nothing"

Sign up to request clarification or add additional context in comments.

5 Comments

It still doesn't work; Get-ChildItem : A parameter cannot be found that matches parameter name 'Directory'.
Tried this and works perfectly Get-ChildItem -Path $BackupPath | Where-Object { (((get-date) - $_.CreationTime).days -ge 7) -and ($_.PsISContainer -eq $True)} | Remove-Item -Recurse
Great!....i think you have a powershell version that dosent have the -directory parameter.
I thought it was working fine, but now I noticed that if I place folders, within the folders it doesn't delete them even if I use Recurse. Remove-Item : Cannot remove item C:\UKXHQ06WKS05GPOBackup\24-09-15\{04AD9BB4-9694-40FE-836B-708C5ADCA361}\DomainSysvol\GPO\Adm\admfiles.ini: Not Enough permission to perfo rm operation. Remove-Item : Directory C:\UKXHQ06WKS05GPOBackup\24-09-15\{04AD9BB4-9694-40FE-836B-708C5ADCA361}\DomainSysvol\GPO\Adm cannot be removed because it is not empty.
I had to add -Force, to sort it: Tried this and works perfectly Get-ChildItem -Path $BackupPath | Where-Object { (((get-date) - $_.CreationTime).days -ge 7) -and ($_.PsISContainer -eq $True)} | Remove-Item -Recurse -Force

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.