18

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?

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

2 Answers 2

28

Just run

pwsh testscript_writefile.ps1

instead of

powershell testscript_writefile.ps1

The powershell command launches PowerShell 5 which is based on the Windows-only .NET framework. The newer pwsh launches the newer, cross-platform .NET core version of PowerShell (version 6+). Separate commands are used to ensure backwards compatibility when the powershell command is used and avoids confusion between PowerShell versions in scripts and code samples.

7
  • 5
    to make this absolutely correct answer even better, explain why this is the correct answer
    – SimonS
    Commented May 29, 2020 at 11:32
  • @SimonS Basically the executable command has changed names. If you call powershell.exe you call the old one, and if you call pwsh.exe then you call the new one... Commented Apr 9, 2021 at 11:13
  • 2
    @SimonS I tried an edit with more detail, which was rejected, so super fast: Check Mark Kraus' blog, which explains pwsh.exe is v6.0+, .NET Core & crossplatform while powershell.exe is .NET Framework & Windows-only. Kraus quotes a GitHub comment from Steve Lee, PowerShell Mgr: The benefit is making it explicit without ambiguity on Windows [which] you are using
    – ruffin
    Commented Oct 27, 2021 at 17:11
  • pwsh.exe -f filename.ps1 to be exact
    – clhy
    Commented Feb 5 at 12:18
  • 1
    In order for me to get this to work, I had to use the fully qualified address for Powershell, i.e.: "C:\Program Files\PowerShell\7\pwsh.exe". Only then was I actually running PowerShell (7.4.3) from the command prompt.
    – ouflak
    Commented Jul 7 at 15:04
8

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

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

pwsh [UNC to your 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 run code using the OS default...

Start-ConsoleCommand -ConsoleCommand 'some command string'

... run the code in PSCore...

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

You could do something similar.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .