Hi, I would like to ask for your assistance in creating a script to find and remove two unwanted files on our Workstations. I can use a one liner using G-tchilditem -Recurse and then Remov-item. but I’ve realized that I need to add a bit more logic so the script only executes if the result of the Select-string-paten is not a successful $lastExitCode “0”.
$filea = C:\users\ * (This file can exist anywhere within a users profile.)
$Fileb = C:* (This file can exist anywhere in the C Drive)
$checkfile = C:\apps\Checkfile.txt
1 The first time the script will execute with no porbome because there is no check file.
2 On subsequent times the script would need to read the contents of the checkfile.txt select-string -pattern “0”
3. The Remove-Item command would execute and the check file would be created with Add-content $lastexitCode
My problem is developing the logic to determine whether the script needs to run or no t based on the content of the checkfile.txt.
Would I use the Test-path to determine the existence of the Checkfile?
Thank your your help Olaf. I’d like to ask one more question. This code works perfectly if the checkfile.txt file exits. How can I make it work if the checkfile.txt does not exit? It makes sense to me that if the file does not exist , skip the select-string pattern match and execute the Get-childitem | Remove-item portion of the code. Once again thank you for your help.
Wait. What? If you want to run the code regardless of any condition you just run the code without any condition!?
What’s the point to add a condition if you want to run the code anyway?