Link to home
Create AccountLog in
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}
SOLUTION
Avatar of wls3
wls3
Flag of United States of America image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
SOLUTION
Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Avatar of 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
Avatar of ltrcne
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
Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Avatar of ltrcne

ASKER

Script is now working exactly as I wanted.  Thanks
Chris,

I know I've seen (and used) the $($somevar) syntax before.  What precisely does this do?
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
Input:

$Name.length

Output:

4

Input:

"$Name"

Output:

Dale

Input:

"$Name.length"

Output:

Dale.length

Input:

"$($Name.length)"

Output:

4


-Dale Harris
Thanks Dale.  That sheds some light on it.
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