-1

I currently have a batch file that looks to see if a folder exists and then if it does it runs the uninstall exe within it and then installs the program with an msi. I need to alter it or just convert it to powershell so that it first checks the age of the folder before continuing. The age I was thinking I would use would be older than 12 months. A problem that I kept running into was that I need to use a wildcard to check if the directory exists due to it being slightly different on each computer at the end of the name. Here is what I have so far with nothing related to age included.

@echo off
IF exist C:\ProgramData\bomgar*  ( goto Uninstall ) ELSE ( goto Install )

:Uninstall
cd C:\ProgramData\bomgar* 

remove.exe

:Install
msiexec /i "C:\pathtomsi" 

:End
Echo Test Complete

Any help or advice would be greatly appreciated.

1

1 Answer 1

0

In Powershell, you can use the wild card the same. You can also complete the date comparisons by adding negative 12 months:

$folders = Get-Item "C:\ProgramData\bomgar*" | Where-Object {
   $_.PSIsContainer -and $_.CreationTime -le (Get-Date).AddMonths(-12)
}

If it returns a result then uninstall, otherwise install:

If ($folders) {
   Foreach ($folder In $folders) {
      "UNINSTALL $($folder.FullName) - $($folder.CreationTime)"
   }
} Else {
   "INSTALL $($folder.FullName) - $($folder.CreationTime)"
}

Edit: there was possibly a logic error in my greater than or equal to (-ge). Try changing it to -le. I also added output lines so there is some visibility on what folders it is selecting - just to help you debug.

1
  • this seems to remove the program whether is was installed greater than 12 months ago or not, I ran the code and it runs the install process correctly but as soon as it detects that folder in program data it removes it even if it was installed today do you know why it would be doing this?
    – Thomas
    Oct 15, 2015 at 14:33

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .