r/PowerShell • u/BurberryC06 • Sep 16 '26
Question Powershell Script won't run at startup (but works when the task is started manually)
EDIT: Now solved. Either was caused by power options being enabled (even though AC was connected) or changing trigger from 'startup' to 'logon'.
I have a simple PS script that turns on Mobile Hotspot automatically. The only conditional check is to see if there is ethernet connected. It was AI authored but I have run this script manually and also checked it works by running the task post-startup (mobile hotspot can be confirmed running).
The task scheduler shows the last run time as the last time the task was run manually so I assume the Task itself was just never started rather than a script execution error. Why isn't it running the startup task?
Here is the powershell code:
# 1. Define WinRT types
$NetworkInfoType = [Windows.Networking.Connectivity.NetworkInformation, Windows.Networking.Connectivity, ContentType=WindowsRuntime]
$TetheringManagerType = [Windows.Networking.NetworkOperators.NetworkOperatorTetheringManager, Windows.Networking.NetworkOperators, ContentType=WindowsRuntime]
# 2. Get active connection profile
$connectionProfile = $NetworkInfoType::GetInternetConnectionProfile()
# 3. Verify connection is valid and is Ethernet (IanaInterfaceType 6)
if ($connectionProfile -and $connectionProfile.NetworkAdapter.IanaInterfaceType -eq 6) {
# 4. Initialize tethering manager
$wifiManager = $TetheringManagerType::CreateFromConnectionProfile($connectionProfile)
# 5. Trigger the hotspot activation asynchronously
$asyncOperation = $wifiManager.StartTetheringAsync()
# 6. Wait for the background WinRT process to finish its work (Status 0 = Started/Running)
while ($asyncOperation.Status -eq 0) {
Start-Sleep -Milliseconds 200
}
}The action is 'start a program', trigger 'at startup', execution command is
powershell.exe -ExecutionPolicy Bypass -File "C:\StartupScripts\TurnOnHotspot.ps1"
7
u/Haitz4 Sep 16 '26
Im not so sure how windows handles network adapter settings at startup, but im gonna take a wild guess that it can't do what you want at this system state, soooo I suggest changing the trigger to "on logon".
3
u/dathar Sep 16 '26
Have you tried any of the wonderful task scheduler troubleshooting steps?
1
u/CookinTendies5864 Sep 16 '26
You have to make the args in task scheduler the execution policy and script.
File: powershell.exe
Args: -ExecutionPolicy Bypass -File "C:\StartupScripts\TurnOnHotspot.ps1"
1
u/Accurate-Bicycle1254 Sep 17 '26
You could also trigger the start of your ps script after logon by creating a .bat file with the content: powershell.exe -executionpolicy bypass -file "c:\startupscripts\turnonhotspot.ps1" ,and put that .bat file into your C:\Users\yourusername\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup folder. You can also access this folder by pressing windows + R or manually open the Run dialog window and then type: shell:startup
But if it works for you now then take this as an extra info if you did not know about this before 😄
1
u/Automatic-Let8857 Sep 16 '26
Under which user it is running? If it is startup task, try using SYSTEM user. Also look into "Start the task only if the computer is on AC power" - uncheck it.
1
u/Automatic-Let8857 Sep 16 '26
Under which user it is running? If it is startup task, try using SYSTEM user. Also look into "Start the task only if the computer is on AC power" - uncheck it.
1
u/Andrew-Powershell Sep 16 '26
Yeah those power settings are annoying. Bad UX