Why is my script slower when launched from the Windows Context Menu?

0
16
Asked By CuriousGiraffe73 On

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

Answered By CodeCrafter56 On

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.

Answered By ScriptSleuth45 On

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.

Answered By PythonPioneer89 On

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.

Answered By DevDude88 On

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.

Answered By TechWhiz42 On

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

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.