troubleshooting Question

Powershell Path Variables

Avatar of ltrcne
ltrcne asked on
Scripting LanguagesPowershell
11 Comments3 Solutions732 ViewsLast Modified:
I would like to use a powershell script to delete all PDF files from user folders that are older than 30 days.  I'm using the get-childitem command and need to look in a specific folder for each user.  Is there a way to use a variable in the target folder path like E:\users\%username%\documents\scans"?

The script below works like I want but only for the one specified user folder.  I need it to run for all user folders.

$Now = Get-Date
$Days = "30"
$TargetFolder = "E:\Users\john\My Documents\Scanned Documents"
$LastWrite = $Now.AddDays(-$days)
$Files = get-childitem $Targetfolder -include *.pdf -recurse | Where {$_.LastWriteTime -le "$LastWrite"}
foreach ($File in $Files)
{write-host "Deleting File $File" -foregroundcolor "Red"; Remove-Item $File | out-null}
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 3 Answers and 11 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 3 Answers and 11 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros