When using a Powershell script in a scheduled task, external command, shortcut or script, always use the –noprofile commandline parameter to start the PowerShell script.
A typical commandline looks like this:
powershell.exe –executionpolicy bypass –noprofile –windowstyle hidden –file <filename>.ps1
Without the noprofile parameter, PowerShell loads, if present, all the profile scripts before executing the script. This will take some time before the actual script is processed and it can also be very dangerous because you do not know what is inside the profile scripts.
A PowerShell profile script is like an autoexec.bat for the PowerShell console, it is processed every time when PowerShell is started. You can use the script to customize the PowerShell environment like loading libraries, changing background colors, checking the weather etc. The variable $profile will list all the configured profile scripts.
Happy scripting!
Photo by Ilya Pavlov on Unsplash