0

I am trying to write a script that will look at a folder, %userprofile%\SCRATCH, and provide the user with a pop up window if they have files in there that are more than two weeks old.

This is a script that will be run at logon. I am using C:\Scratch as a test, but I don't know how to use Windows variables in PowerShell.

$LogPath = "C:\SCRATCH" # Where to look for files
$Daysback = "-14" # Defines file age limit
$CurrentDate = Get-Date # Gets date created for current files.
$DatetoDelete = $CurrentDate.AddDays($Daysback) # Gets the date of the folder/files

Get-ChildItem $LogPath | Where-Object { $_.LastWriteTime -gt $DatetoDelete

If ($Datetodelete -gt 14)
{
    $a = new-object -comobject wscript.shell
    $b = $a.popup(“Delete Test“,0,”Good Job!”,1)
}

else
{
    $a1 = new-object -comobject wscript.shell
    $b1 = $a.popup(“GTG Test“,0,”Good Job!”,1)
}

Something is wrong since I just get the "Delete Test" no matter what number I use in the If statement.

4
  • Looks like you're missing a closing curly brace on the end of the line that starts with Get-ChildItem. Commented Dec 12, 2013 at 20:17
  • $env:USERPROFILE will give you windows user profile path (C:\Users\Travis, for example). Commented Dec 12, 2013 at 20:48
  • I had seen the $env:USERPROFILE on here before but what I"m not sure of is how to then append another folder to USERPROFILE. I need to check the SCRATCH folder in the user's profile. Commented Dec 12, 2013 at 20:56
  • David, Nice catch but i just failed at copying and pasting. Commented Dec 12, 2013 at 21:00

1 Answer 1

1

I assume that your intent in the if statement was to compare $Datetodelete with some date offset using the integer 14. However, you are actually comparing to the integer 14, instead.

$Datetodelete is a DateTime object, so, if my assumption is correct, you need to compare it with another DateTime object in your if statement, instead of an integer.

Sign up to request clarification or add additional context in comments.

2 Comments

Would the $DatetoDelete variable be another DateTime object?
That's what I said. $Datetodeleteis a DateTime object. $Datetodelete is the result of an operation that you performed on $CurrentDate, which in turn was the result of calling Get-Date, which returns a DateTime object. You can prove this to yourself by adding the line Write-Output $Datetodelete.gettype().Name to your code, immediately after the line where you assign $Datetodelete.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.