1

Executing

Set-PSRepository -Name 'PSGallery' -InstallationPolicy Trusted

in a Powershell 5 session,

Question

  • will its effect remain in subsequent new sessions?
  • If later I install a Powershell Core 7.x.x will those session affected?

(In general it is not clear for me how side by side installed PowerShell 5 and PowerShell Core 7.x.x how cross side effected in 5->7 and 7->5 direction)

g.pickardou
  • 32,346
  • 36
  • 123
  • 268
  • 2
    The Repository is a per-user setting and is persistent. No idea across different PS versions but proabably not. You can test yourself using `Get-PSRepository` – Scepticalist Feb 26 '21 at 12:15
  • Run the command in Powershell 5, then install a package from that repository. Go to Powershell 7, then try the same command. If it worked, then you have your answer. If it didn't work, you also have your answer. To both questions. – WaitingForGuacamole Feb 26 '21 at 12:39
  • @WaitingForGuacamole: It is not so simple...There are many use case, regarding the direction of the effect and the timeline of the events. For example having PS 5, then Set-PSRepository, then install PS 7, -or- having PS 5, then install PS 7, then Set-PSRepository 7, -or- install it in PS 7, etc. If we want reproduceable results (one of the mandatory attribute of scripting) must know our tool. – g.pickardou Feb 27 '21 at 06:46

1 Answers1

2

As stated in the documentation for Set-PSRepository and Register-PSRepository, any changes are persistent and in the user scope. They apply to all versions of PowerShell, which includes different editions. This means that PowerShell Core 6.x/PowerShell 7.x will also see those changes for the same user, even if not installed when the changes were made. This also extends to any other PS repositories that you change/register.

I have spun up a test instance and independently verified this:

  • Windows PowerShell 5.1
PS C:\Users\TestUser01> Get-CimInstance -Query 'SELECT Caption,Version FROM Win32_OperatingSystem' | select Caption,Version

Caption                              Version
-------                              -------
Microsoft Windows 10 Enterprise LTSC 10.0.17763


PS C:\Users\TestUser01> $PSVersionTable.PSVersion.ToString()
5.1.17763.1490
PS C:\Users\TestUser01> $null -eq (gcm pwsh -ea:ig)
True
PS C:\Users\TestUser01> Get-PSRepository

Name                      InstallationPolicy   SourceLocation
----                      ------------------   --------------
PSGallery                 Untrusted            https://www.powershellgallery.com/api/v2


PS C:\Users\TestUser01> Set-PSRepository PSGallery -in Trusted
PS C:\Users\TestUser01> Register-PSRepository ExampleRepo -so 'http://example.com'
PS C:\Users\TestUser01> Get-PSRepository

Name                      InstallationPolicy   SourceLocation
----                      ------------------   --------------
PSGallery                 Trusted              https://www.powershellgallery.com/api/v2
ExampleRepo               Untrusted            http://example.com/
  • Installed PowerShell 7.1.2 x64
PS C:\Users\TestUser01> $PSVersionTable.PSVersion.ToString()
7.1.2
PS C:\Users\TestUser01> Get-PSRepository

Name                      InstallationPolicy   SourceLocation
----                      ------------------   --------------
PSGallery                 Trusted              https://www.powershellgallery.com/api/v2
ExampleRepo               Untrusted            http://example.com/
TheFreeman193
  • 467
  • 1
  • 7
  • 14
  • 1
    Many thanks. Maybe it is may bad, but I did not discover this in the docs. My doubt started when I executed it in a remote context, then it eemed not to remain in the next context. – g.pickardou Feb 26 '21 at 20:08