Resolving winget not recognized error when running with the System Account

Although winget exists on your system, but when you try to run the winget with system account (or using the scheduled task with the system account) and you see this error.

winget : The term ‘winget’ is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

(Note: If the ‘winget’ is not installed or is corrupted on your system, you can refer to this post for instructions on how to install it.)

The problem is the System Account in Windows does not have a profile like a regular user account. When programs or scripts run under the System Account, they do not load a user-specific profile with environment variables like PATH. Instead, they use a default set of system-wide environment variables, which typically includes directories where system executables and utilities are located, such as %SystemRoot%\system32 and %SystemRoot%.

The solution is to add your winget directory path into the system-wide variable as shown in the fig. Find the winget directory with the following powershell command.

(Get-ChildItem -Path $env:ProgramFiles\WindowsApps\Microsoft.DesktopAppInstaller*_x64*\winget.exe).DirectoryName

I also made a powershell function that adds the winget path into the current powershell session variable $Env:Path

Although adding the winget path into the session variable works well for the current powershell session or script, it does not persist across reboots unless you add it to the registry. For this, I also made a function to add the path into the registry.

Leave a Reply

Your email address will not be published. Required fields are marked *