PowerShell

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.

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:

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>

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.

get-module -ListAvailable PowerShellGet

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

Przemyslaw Klys

System Architect with over 14 years of experience in the IT field. Skilled, among others, in Active Directory, Microsoft Exchange and Office 365. Profoundly interested in PowerShell. Software geek.

Share
Published by
Przemyslaw Klys

Recent Posts

Upgrade Azure Active Directory Connect fails with unexpected error

Today, I made the decision to upgrade my test environment and update the version of…

2 miesiące ago

Mastering Active Directory Hygiene: Automating Stale Computer Cleanup with CleanupMonster

Have you ever looked at your Active Directory and wondered, "Why do I still have…

3 miesiące ago

Active Directory Replication Summary to your Email or Microsoft Teams

Active Directory replication is a critical process that ensures the consistent and up-to-date state of…

7 miesięcy ago

Syncing Global Address List (GAL) to personal contacts and between Office 365 tenants with PowerShell

Hey there! Today, I wanted to introduce you to one of the small but excellent…

12 miesięcy ago

Active Directory Health Check using Microsoft Entra Connect Health Service

Active Directory (AD) is crucial in managing identities and resources within an organization. Ensuring its…

1 rok ago

Seamless HTML Report Creation: Harness the Power of Markdown with PSWriteHTML PowerShell Module

In today's digital age, the ability to create compelling and informative HTML reports and documents…

1 rok ago