Search and Remove files based on last exit code

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?

Hopefully I am making sense.

Thank you

Welcome to the forums.

I’m unsure if I really understood your logic. How do you identify the files to delete and what are the conditions for the files to be deleted?

Regardless of that - could you please format your code as code here in the forum?
Thanks in advacne.

1 Like

Hi, sorry for the confusion . This is what would like to acomplish.

$sestr = "ExitCode:0"
$sefile = 'C:\apps\Checkfile.txt'

# If $sefile exists, read content and search for pattern $sestr

    if (Test-path -path $sefile)
 $seresults = Get-Content $sefile | Select-String -Pattern $sestr
        Get-ChildItem -path C:\ -Recurse -ErrorAction SilentlyContinue | Where {$_.Name -Match "Filea123.txt"} | Remove-Item -Force

        New-Item -ItemType file $sefile -Value "ExitCode:$lastexitcode"

Hmmm … I’m still unsure if I got it. But it seems like you’ve got what you wanted, right? :wink:

Anyway … you could streamline your code a little bit like this: :wink:

$sestr  = 'ExitCode:0'
$sefile = 'C:\apps\Checkfile.txt'
if (Test-Path -Path $sefile) {
    if (-not (Select-String -Path $sefile -Pattern $sestr -SimpleMatch -Quiet)) {
        Get-ChildItem -Path C:\ -Filter 'Filea123.txt' -Recurse -ErrorAction SilentlyContinue | 
            Remove-Item -Force
        New-Item -ItemType File $sefile -Value "ExitCode:$lastexitcode"

Do you really want to search your complete C: drive? I’d recommend to exclude certain folders or even better to include only the possible options.

1 Like

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!? :thinking:
What’s the point to add a condition if you want to run the code anyway?