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, 2020 at 4:20

2 Answers 2


Just run

pwsh testscript_writefile.ps1

instead of

powershell testscript_writefile.ps1
  • 3
    to make this absolutely correct answer even better, explain why this is the correct answer
    – SimonS
    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... Apr 9, 2021 at 11:13
  • @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
    Oct 27, 2021 at 17:11

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

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

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.

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
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



    If ($PoSHCore)
    {Start-Process pwsh -ArgumentList "-NoExit","-Command  &{ $ConsoleCommand }" -Wait}
    {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.

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.