I've set up a context menu button that runs a PowerShell script, which in turn executes a Python script. The idea is to make it easier for users to run Python files by right-clicking without having to open the command line. However, I've noticed a significant slowdown: the script takes about 1 minute to run from an open terminal, but when launched from the context menu, it takes almost 4 minutes. The script includes two main parts—one that's CPU-bound and another that's I/O bound, both slow down similarly. I'm suspecting it might have to do with Windows Defender's real-time protection being harsher on scripts initiated from the context menu, but my corporate setup prevents me from testing this theory. Has anyone faced a similar issue or have any ideas on what could be causing this slow performance?
5 Answers
Consider this: since you're on a corporate laptop, you might have limited access to power settings. Try adjusting how you call your Python script from the context menu? For instance, using `powershell.exe -c "python.exe "` or even using `cmd.exe` could be options. Running the shell directly might help optimize performance.
You might want to try adding the `-noprofile` switch when you call PowerShell. It could help speed things up by preventing the loading of the user profile.
Could you let us know what version of Windows and Python you're using? That might help pinpoint if there are any known issues with certain versions.
You could also log performance with `New-MpPerformanceRecording` to gather data on what might be causing the delay. After that, use `Get-MpPerformanceReport` for analysis or dive deeper with PerfView from GitHub. Also, try logging with ProcMon to see if there are specific timestamps that indicate delays when launching from the context menu. I doubt it's actually related to Defender, though.
Have you tried using any Sysinternals tools to profile the script when it runs? Resource Monitor could help you see what's happening in the background. You might also want to check the execution timing within your script to see if anything stands out.

Related Questions
How To: Running Codex CLI on Windows with Azure OpenAI
Set Wordpress Featured Image Using Javascript
How To Fix PHP Random Being The Same
Why no WebP Support with Wordpress
Replace Wordpress Cron With Linux Cron
Customize Yoast Canonical URL Programmatically