2

I have a script that I need to run AS Powershell 7 because only Powershell 7 has the command I need.

When I run $PSVersionTable in a .PS1 script from VSCode I get:

Name                           Value
----                           -----
PSVersion                      7.0.1
PSEdition                      Core

When I run that same .PS1 script from command prompt using powershell testscript_writefile.ps1, I get:

Name                           Value
----                           -----
PSVersion                      5.1.14393.3471
PSEdition                      Desktop

How can I execute a Powershell script AS Powershell 7 instead of Powershell 5?

  • Either use alternative cmdlet or create your own alternative ps module or install PS7. – Biswapriyo May 28 at 4:20
3

Just run

pwsh testscript_writefile.ps1

instead of

powershell testscript_writefile.ps1
| improve this answer | |
  • to make this absolutely correct answer even better, explain why this is the correct answer – SimonS May 29 at 11:32
1

When you run your script, it will run int the default OS Powershell host, and on Windows that will be default OS version Windows PowerShell, unless you specifically telly it otherwise.

If you want to run the script with PSCore, the type...

pwsh [UNC to you script here.]

... what you have configured VSCode to use as the default PS version has no impact on what the OS will use.

Your other item to ensure that your code will only try and run in PSCore is that you use the #requires statement at the top of all your scripts.

about_Requires - PowerShell | Microsoft Docs

This does not mean you could autorun in PSCore, it will throw an error telling the user the defined PS version must be used. So, they can run using the right version by starting or typing in the right version.

Code in the sample script

#Requires -Version 7.0
"Running $PSVersionTable"

Running the script

PS C:\> $PSVersionTable
<#
#Results

Name                           Value
----                           -----
PSVersion                      5.1.18362.752
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.18362.752
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
<#

PS C:\> D:\Scripts\TestForHostVersion.ps1   
<#
# Results
                                                                        D:\Scripts\TestForHostVersion.ps1 : The script 'TestForHostVersion.ps1' cannot be run because it contained a
"#requires" statement for Windows PowerShell 7.0. The version of Windows PowerShell that is required by the script
does not match the currently running version of Windows PowerShell 5.1.18362.752.
At line:1 char:1
+ D:\Scripts\TestForHostVersion.ps1
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ResourceUnavailable: (TestForHostVersion.ps1:String) [], ScriptRequiresException
    + FullyQualifiedErrorId : ScriptRequiresUnmatchedPSVersion
#>

PS C:\> pwsh -noprofile   
<#
# Results


                                                                                PowerShell 7.0.1
Copyright (c) Microsoft Corporation. All rights reserved.

https://aka.ms/powershell
Type 'help' to get help.
#>

PS C:\> D:\scripts\TestForHostVersion.ps1
<#
# Results

Running System.Management.Automation.PSVersionHashTable
#>

PS C:\> $PSVersionTable
<#
# Results

Name                           Value
----                           -----
PSVersion                      7.0.1
PSEdition                      Core
GitCommitId                    7.0.1
OS                             Microsoft Windows 10.0.18363
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

PS C:\> exit
#>

The other option is to fix your code to check for the host version and the branch to the correct version. For example, I have a function I use to run a code block based on the version I want to use. I keep this in my module that is imported via my profile so that it is always available.

Function Start-ConsoleCommand
{
    [CmdletBinding(SupportsShouldProcess)]

    [Alias('scc')]

    Param  
    ( 
        [string]$ConsoleCommand,
        [switch]$PoSHCore
    )

    If ($PoSHCore)
    {Start-Process pwsh -ArgumentList "-NoExit","-Command  &{ $ConsoleCommand }" -Wait}
    Else
    {Start-Process powershell -ArgumentList "-NoExit","-Command  &{ $ConsoleCommand }" -Wait}

}

So, to tun code using the OS default...

Start-ConsoleCommand -ConsoelCommand 'some command string'

... run the code in PSCore...

Start-ConsoleCommand -ConsoelCommand 'some command string' -PoshCore

You could do something similar.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.