E-Money
E-Money

Reputation: 1

PowerShell If/Else

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.

Upvotes: 0

Views: 1141

Answers (1)

David
David

Reputation: 6571

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.

Upvotes: 1

Related Questions