Graph and PowerShell Blog | About | Links
Moving from PowerShell 5.1 to Core
12-Jul-24

On my Management Server, I have nearly always used PowerShell 5.1 for scripts. Exchange 2016 will only work with the snap-in called from 5.1 and is not compatible with PowerShell 7 (Core), because of this the scripts that don't require Exchange used 5.1 to make it easier to administer.

Recently though, I've noticed a few scripts no longer working as I update the modules. While I could go back and try to fix the issues it seems to make more sense now to start working towards moving the scripts to PowerShell Core, especially as I will shortly decommission my on-prem Exchange servers.

PowerShell Core is not without it's issues though, especially if you use a service account, with scheduled tasks, with an authenticated web proxy :(. The main issues I had to troubleshoot were:

  • Replace PowerShellGet with PSResource
  • Install modules for all users
  • Converting scheduled tasks from 5.1 to Core
  • Other general tips

  • Replace PowerShellGet with PSResourceGet
    Installing Modules with the default v1 PowerShellGet module is a frustrating experience, more so if you only have TLS 1.2 enabled on your server and have an authenticated proxy. PSResourceGet is actually version 3 of PowerShellGet but the owners decided to rename it. Before getting into the installation, check your Internet connection is up and running:

    [PowerShell Core run as Admin]
    ==============================
    [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
    [System.Net.Http.HttpClient]::DefaultProxy.Credentials = [System.Net.CredentialCache]::DefaultCredentials # Use local credentials for proxy authentication

    Invoke-WebRequest -uri https://google.com

    To install run the following:

    Install-Module -Name Microsoft.PowerShell.PSResourceGet -RequiredVersion 1.0.5


    Install modules for all users
    When installing modules you should install them for all users, this means if you run scheduled tasks using a different account it will use the same module that you would use in testing:

    Install-PSResource -Name MicrosoftTeams -Scope AllUsers
    Update-PSResource PSFramework -Scope allusers
    Update-PSResource PackageManagement -Scope allusers

    One thing to note is when you update a module it will leave the old version installed, so it's probably a good idea to cleanup the old versions with the following command:

    Uninstall-PSResource PSFramework -Version 0.9.25.107 -Scope allusers -SkipDependencyCheck

    Finally, if we want to list the installed modules and also check if there is a newer version we have these commands:

    Get-InstalledPSResource -Scope AllUsers
    Find-PSResource -Name az -Repository PSGallery


    Converting scheduled tasks from 5.1 to Core
    When creating scheduled tasks that use PowerShell I always use Notepad to write down the commands before editing the task, this prevents typos and other types of errors. It's also useful to have the service account password handy as you will need it for each edited task. The only changes you will need to make are changing the PowerShell executable and the script command:

    Program: "C:\Program Files\PowerShell\7\pwsh.exe"
    Arguments: -ExecutionPolicy ByPass -File "C:\Scripts\Exchange\script.ps1"

    ↑ The 'Arguments' field is very small, so better to edit in Notepad first.


    Other General Tips
  • If using the module Microsoft.Graph in scripts, then don't import the module. The Connect-MGGraph command will already know what features the Azure app can access. If you do import the module, then it can take up to 15 minutes to load all the different parts.
  • With PowerShell Core the web proxy is automatically detected as per your Internet Settings, but if you use an authenticated proxy then you will want to add the following to your PowerShell profile: [System.Net.Http.HttpClient]::DefaultProxy.Credentials = [System.Net.CredentialCache]::DefaultCredentials