# Set-PSRepository Installation Policy Trusted – Invalid Class Error

I have to admit – executing the same command three times and expecting different results is dumb, but I still do it anyway. Fortunately, after trying three times I usually resort to other methods and try to solve the problem I have. Today while trying to trust PowerShellGallery I was greeted with an error.

blog

21 Nov 2019 en

![Set-PSRepository Installation Policy Trusted – Invalid Class Error](/wp-content/uploads/2019/11/img_5dd65971ebc5b.png)

[View images (3)](#pf-content-image-1-0)

I have to admit – executing the same command three times and expecting different results is dumb, but I still do it anyway. Fortunately, after trying three times I usually resort to other methods and try to solve the problem I have. Today while trying to trust PowerShellGallery I was greeted with an error.

[!\[Set-PSRepository error output showing the Test-Connection invalid class failure from PowerShellGet 1.0.0.1.\](/wp-content/uploads/2019/11/img_5dd65971ebc5b.png)](/wp-content/uploads/2019/11/img_5dd65971ebc5b.png)

> Microsoft.PowerShell.Management\Test-Connection : Invalid class At C:\Program Files\WindowsPowerShell\Modules\PowerShellGet\1.0.0.1\PSModule.psm1:6094 char:22
> 
> - … connected = Microsoft.PowerShell.Management\Test-Connection -Computer …
> - CategoryInfo : InvalidOperation: (:) [Test-Connection], ManagementException
> - FullyQualifiedErrorId : TestConnectionException,Microsoft.PowerShell.Commands.TestConnectionCommand

The error is cryptic – **Invalid Class** for a **Test-Connection** during **Set-PSRepository** trusted installation policy. That's not good, and not really an error that is obvious. Fortunately, I read the whole error and noticed one little thing. The error was coming from **PowerShellGet 1.0.0.1** version. One thing to know about **PowerShellGet** is that it comes bundled in on newer systems or when you install **PowerShell 5.1**. And just like **Pester** it's heavily outdated at the start.

### 💡 Set-PSRepository – Invalid Class Solution

Instead of trying to analyze the error, go thru code and see where it's failing the first instinct that kicks in is to just update that **PowerShell Module**. So what you do is to run 2 little commands:

```powershell
PS C:\Windows\system32> Set-PSRepository -name PSGallery -InstallationPolicy Trusted
Microsoft.PowerShell.Management\Test-Connection : Invalid class
At C:\Program Files\WindowsPowerShell\Modules\PowerShellGet\1.0.0.1\PSModule.psm1:6094 char:22
+ ... connected = Microsoft.PowerShell.Management\Test-Connection -Computer ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [Test-Connection], ManagementException
    + FullyQualifiedErrorId : TestConnectionException,Microsoft.PowerShell.Commands.TestConnect

PS C:\Windows\system32> install-module PowershellGet -Force
PS C:\Windows\system32> import-module powershellget -force
PS C:\Windows\system32> Set-PSRepository -name PSGallery -InstallationPolicy Trusted
PS C:\Windows\system32>
```

[!\[PowerShell console sequence installing a newer PowerShellGet module and retrying Set-PSRepository successfully.\](/wp-content/uploads/2019/11/img_5dd65a2ae0fdb.png)](/wp-content/uploads/2019/11/img_5dd65a2ae0fdb.png)

And as I thought, it worked! You may wonder why I was using **Install-Module** instead of **Update-Module**. This is because you can't update module that wasn't installed from PowerShellGallery.

```powershell
get-module -ListAvailable PowerShellGet
```

[!\[Get-Module -ListAvailable output showing both the bundled and newer PowerShellGet versions on the system.\](/wp-content/uploads/2019/11/img_5dd6653218e11.png)](/wp-content/uploads/2019/11/img_5dd6653218e11.png)

As you can see now, we have two versions of **PowerShellGet**, but we use only the newest one.

## Related Projects

Projects that align with this post's tags, categories, and content.

[All projects](/projects/)

Matching projects...

[Back to Blog](/blog)

## Related posts

- [Install-module : The ‘install-module’ command was found in the module ‘PowerShellGet’, but the module could not be loaded.](/blog/install-module-the-install-module-command-was-found-in-the-module-powershellget-but-the-module-could-not-be-loaded/)
- [Function cannot be created because function capacity 4096 has been exceeded for this scope](/blog/function-cannot-be-created-because-function-capacity-4096-has-been-exceeded-for-this-scope/)
- [Install-Module : A parameter cannot be found that matches parameter name AllowPrerelease.](/blog/install-module-a-parameter-cannot-be-found-that-matches-parameter-name-allowprerelease/)

<!-- PowerForge markdown artifact sha256:9294BC72D65887C786CB843BFB39D982E5E6679294440A0086B0B18A91D3FEE4 -->
