powershell where subfolder contains string

Hi

I need to write a powershell script to remove files older than a certain date but only from sub folder with the name "working" in it

I have found the code below but just need to modify for the folder name

any help is greatly appreciated

code from - http://www.winblogs.net/index.php/2009/10/01/delete-files-older-from-in-powershell/ 



a
Function GetOldFile
{
$Days = "0"
$TargetFolder = "d:\"
if (Test-Path $TargetFolder)
{
$Now = Get-Date
$LastWrite = $Now.AddDays(-$days)
$Files = get-childitem $TargetFolder -include *.john -recurse |Where {$_.LastWriteTime -le "$LastWrite"}
foreach ($File in $Files)
{write-host "Deleting file $File" -foregroundcolor "Red"; Remove-Item $File | out-null}
}
Else
{Write-Host "Folder $TargetFolder doesnt exist! Recheck the folder path!"}
}
GetOldFile

Open in new window

wbcintsolAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Meir RivkinFull stack Software EngineerCommented:
try this code:
Function GetOldFile
{
	$Days = "0"
	$TargetFolder = "d:\"
	if (Test-Path $TargetFolder)
	{
		$Now = Get-Date
		$LastWrite = $Now.AddDays(-$days)
		$Folders = get-childitem $TargetFolder -include *working* -recurse |Where {$_.psIsContainer -eq $true}
		foreach ($Folder in $Folders)
		{
			write-host "Folder - $Folder" -foregroundcolor "Red"
			$Files = get-childitem $Folder | Where {$_.LastWriteTime -le "$LastWrite"}
			foreach ($File in $Files)
			{
				write-host "Deleting file $File" -foregroundcolor "Red"
				Remove-Item $File | out-null
			}
		}
	}
	Else
	{
		Write-Host "Folder $TargetFolder doesnt exist! Recheck the folder path!"
	}
}

GetOldFile

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
wbcintsolAuthor Commented:
Perfect - Thanks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.