# 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 pl

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

[Zobacz obrazy (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.

[!\[Blad Set-PSRepository pokazujacy problem Test-Connection invalid class pochodzacy z 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>
```

[!\[Sekwencja w konsoli PowerShell instalujaca nowszy PowerShellGet i ponawiajaca Set-PSRepository z powodzeniem.\](/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
```

[!\[Wynik Get-Module -ListAvailable pokazujacy zarowno wbudowana, jak i nowsza wersje PowerShellGet na systemie.\](/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.

## Powiązane projekty

Projekty powiązane z tagami, kategoriami i treścią tego wpisu.

[Wszystkie projekty](/projekty/)

Trwa dopasowywanie projektów...

[Powrót do bloga](/blog/)

## Powiązane wpisy

- [Check an API write after a PowerShell timeout](/blog/verify-uncertain-api-outcomes-powershell/)
- [Inspect and convert mixed Office archives with PSWriteOffice](/blog/inspect-and-convert-mixed-office-archives-pswriteoffice/)
- [Compose PDF reports and automate page workflows with PSWriteOffice](/blog/automate-pdf-reports-and-page-workflows-pswriteoffice/)

<!-- PowerForge markdown artifact sha256:A831E17EB9239C7ECFAE4D03E6A6BBB9B129680D44A877632F2179D3C12EAB35 -->
