title: Update-Module not finding PSGallery repo in script as a scheduled task
body: <p>I have a scheduled task which runs an exported function from my PowerShell module which is hosted at <a href="https://powershellgallery.com" rel="nofollow noreferrer">powershellgallery.com</a>. The function performs a check against the built-in PSGallery repository to see if a newer version is available and if so, update it.</p>
  
  <p>I've noticed my module is not being updated as it should and to troubleshoot the issue I've redirected the output from two separate commands. First, to make sure the repository is 'visible' to the <code>SYSTEM</code> account running the task I run:</p>
  
  <pre><code>Get-PSRepository *&gt;&gt; c:\repo.log
  </code></pre>
  
  <p>This yields the following output:</p>
  
  <blockquote>
    <p>Name                      InstallationPolicy   SourceLocation<br>
    ----                      ------------------   --------------<br>
    PSGallery                 Untrusted            <a href="https://www.powershellgallery.com/api/v2" rel="nofollow noreferrer">https://www.powershellgallery.com/api/v2</a>                                </p>
  </blockquote>
  
  <p>So the <code>SYSTEM</code> account running the function as a scheduled task can 'see' the repo; no problem. Next, the function runs the <code>Update-Module</code> command as such:</p>
  
  <pre><code>Update-Module -Name $ProductName -Confirm:$false -Force -Verbose *&gt;&gt; c:\update.log
  </code></pre>
  
  <p>This yields the following output:</p>
  
  <blockquote>
    <p>Checking for updates for module '[removed by me]'.
    PackageManagement\Install-Package : Unable to find repository '<a href="https://www.powershellgallery.com/api/v2/" rel="nofollow noreferrer">https://www.powershellgallery.com/api/v2/</a>'. Use 
    Get-PSRepository to see all available repositories.
    At C:\Program Files\WindowsPowerShell\Modules\PowerShellGet\2.0.4\PSModule.psm1:12546 char:20
    + ...           $sid = PackageManagement\Install-Package @PSBoundParameters
    +                      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : ObjectNotFound: (Microsoft.Power....InstallPackage:InstallPackage) [Install-Package], Ex 
       ception
        + FullyQualifiedErrorId : SourceNotFound,Microsoft.PowerShell.PackageManagement.Cmdlets.InstallPackage</p>
  </blockquote>
  
  <p>Lastly, I checked the module to make sure it is indeed associated with <code>PSGallery</code> by running: </p>
  
  <pre><code>Get-InstalledModule -Name $ProductName | fl
  </code></pre>
  
  <p>The output shows:</p>
  
  <blockquote>
    <p>RepositorySourceLocation   : <a href="https://www.powershellgallery.com/api/v2/" rel="nofollow noreferrer">https://www.powershellgallery.com/api/v2/</a> </p>
    
    <p>Repository                 : PSGallery</p>
  </blockquote>
  
  <p>UPDATE: I decided to use the <code>Install-Module</code> with the <code>-Force</code> switch to 'update' the module instead as I couldn't get the other command to work. Oddly though, when I do a <code>Get-InstalledModule -AllVersions</code> I can clearly see a difference between a module installed interactively and one installed under the <code>SYSTEM</code> account (running as a scheduled task). Pay attention to the <code>Repository</code> column:</p>
  
  <p><a href="https://i.stack.imgur.com/5ySHk.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/5ySHk.png" alt="PowerShell Repo"></a></p>
  
  <ol>
  <li>If I run the function interactively it works without issue.</li>
  <li>If I run <code>Find-Module -Name $ProductName</code> from within the function it finds the module without issue.</li>
  <li>Tried both solutions from <a href="https://stackoverflow.com/questions/53358723/unable-to-find-repository-on-update-module">other question</a> to no avail...</li>
  </ol>
  
  <p>Any idea why the <code>Update-Module</code> command can't find the repo??</p>