Hello,
sorry for the late reply. The command line tool pdf24-DocTool.exe (compression with -compress) is a Windows program with a graphical interface. When such a program is started directly in PowerShell, PowerShell does not wait for it to finish. The next command starts right away, and $? only tells you that the program could be started.
To wait, start the DocTool with Start-Process and the parameters -Wait and -PassThru. PowerShell then waits until the compression has finished, and the ExitCode property of the returned process tells you the result. In a batch file, start /wait does the same.
Meaningful exit codes require the switch -silent, available since version 11.31.0, so please update first if necessary. In silent mode no dialogs are shown, errors are written to stderr, and the exit code is 0 on success and another value on error, e.g. 4 = no output location, 6 = the operation failed. Without -silent, the DocTool always returns 0 and reports errors in a dialog. In silent mode please give the output folder with -outputDir and set the compression with -dpi, -imageQuality and -colorModel (-useCompressionParamsDialog is not available there).
Compressing also works online: https://tools.pdf24.org/en/compress-pdf