Avatar of ltrcne
ltrcne
 asked on

Powershell Path Variables

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}
PowershellScripting Languages

Avatar of undefined
Last Comment
Chris Dent

8/22/2022 - Mon
SOLUTION
wls3

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
SOLUTION
Dale Harris

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Chris Dent

Drop the quotes around "$LastWrite" and let it be a DateTime object, otherwise you'll wind up doing string comparison which will give some pretty fun results.

Chris
ltrcne

ASKER
When I run the modified script i receive the following error for each user:

Get-ChildItem : Cannot find path 'E:\Users\@{Name=John}\My Documents\Scanned Documents' because it does not exist.

Below is my modified script:

$Now = Get-Date
$Days = "365"
$LastWrite = $Now.AddDays(-$days)
$userlist = dir e:\users\* | select name
Foreach ($User in $Userlist){
$TargetFolder = "E:\Users\$User\My Documents\Scanned Documents"
gci $Targetfolder -include "*.pdf" -recurse | ?{$_.LastWriteTime -le $LastWrite} | Remove-Item
}
ASKER CERTIFIED SOLUTION
Log in to continue reading
Log In
Sign up - Free for 7 days
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
ltrcne

ASKER
Script is now working exactly as I wanted.  Thanks
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
wls3

Chris,

I know I've seen (and used) the $($somevar) syntax before.  What precisely does this do?
Dale Harris

This one says

"We are in quotes, so if we want to use a . (period) for a variable property, you must put it in (Parentheses).  But (Parentheses) can be used in a regular string statement, so we must put a $ in front of it to tell powershell to treat the upcoming () as a variable in the middle."

Example:

$Name = "Dale"
"$Name is my name."

"The length of $Name is $($Name.length)"

HTH,

Dale Harris
Dale Harris

Input:

$Name.length

Output:

4

Input:

"$Name"

Output:

Dale

Input:

"$Name.length"

Output:

Dale.length

Input:

"$($Name.length)"

Output:

4


-Dale Harris
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
wls3

Thanks Dale.  That sheds some light on it.
Chris Dent

Officially it declares a sub-expression. You can do some interesting things with it.
# this is the normal output
for ($i = 10; $i -gt 0; $i--) { $i }
# this works
$( for ($i = 10; $i -gt 0; $i--) { $i } ) | Sort-Object
# this does not and will error
for ($i = 10; $i -gt 0; $i--) { $i } | Sort-Object

Open in new window

But Dale is right too, it's very common to see these appear in string concatenation.

Chris