Powershell Path Variables

ltrcne
ltrcne used Ask the Experts™
on
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}
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Commented:
Are the user's names part of a single driver's folder structure?  For instance, are all your users in the E:\users directory?  If so, then you can enumerate these names using this:
$userlist = dir e:\users\* | select name

Open in new window

With that list, you could then do a foreach loop around the existing foreach loop and modify the second folder name in the loop.
Dale HarrisProfessional Services Engineer
Commented:
To add to what WLS3 is saying, it would look something like:


$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
}


See if that works for you.  I didn't do much but clean it up and make it faster.  You might want to add -whatif at the end or -confirm.

HTH,

Dale Harris
Chris DentPowerShell Developer
Top Expert 2010

Commented:
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
Introduction to R

R is considered the predominant language for data scientist and statisticians. Learn how to use R for your own data science projects.

Author

Commented:
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
}
PowerShell Developer
Top Expert 2010
Commented:
You want:

$userlist = dir e:\users\* | Select-Object -ExpandProperty name

If you're going to treat it as a string array, at the moment you have an array of objects with name as the only property for each.

This is neater though.
dir e:\users\* | ForEach-Object {
  $TargetFolder = "E:\Users\$($_.Name)\My Documents\Scanned Documents"
  gci $Targetfolder -include "*.pdf" -recurse | ?{$_.LastWriteTime -le "$LastWrite"} | Remove-Item
}

Open in new window

Chris

Author

Commented:
Script is now working exactly as I wanted.  Thanks

Commented:
Chris,

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

Commented:
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 HarrisProfessional Services Engineer

Commented:
Input:

$Name.length

Output:

4

Input:

"$Name"

Output:

Dale

Input:

"$Name.length"

Output:

Dale.length

Input:

"$($Name.length)"

Output:

4


-Dale Harris

Commented:
Thanks Dale.  That sheds some light on it.
Chris DentPowerShell Developer
Top Expert 2010

Commented:
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

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial