Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use .pwsh instead of .ps1 extension for PowerShell Core scripts?

I am using PowerShell and PowerShell Core in parallel. Not all my scripts which I have wrote for the "big" PowerShell are working in PowerShell Core and vice versa.

To avoid chaos, I was thinking if I should use two file-extensions:

  • .ps1: PowerShell
  • .pwsh: PowerShell Core

In Windows the extension is more important then in Unix systems where the shebang (#!/bin/mytool) would help to solve the problem.

Is the usage of two extension "best practice" or there are better options?

Additional: I am not sure if a .pwsh script will be executed by PowerShell when I call a script from command line / terminal.

I found a similar question in Unix / Linux context. https://unix.stackexchange.com/questions/182882/use-sh-or-bash-extension-for-bash-scripts

like image 636
KargWare Avatar asked Dec 03 '22 18:12

KargWare


1 Answers

Add a line

#requires -PSEdition Core

at the beginning of your scripts for PowerShell Core, and a line

#requires -PSEdition Desktop

at the beginning of your scripts for regular PowerShell.

For scripts that run in both editions just omit the line.

From the documentation:

-PSEdition <PSEdition-Name>

Specifies a PowerShell edition that the script requires. Valid values are Core for PowerShell Core and Desktop for Windows PowerShell.

Do not use different extensions, as that will impact functionality. For instance, PowerShell will not dot-source scripts with an extension other than .ps1.

like image 154
Ansgar Wiechers Avatar answered Dec 06 '22 20:12

Ansgar Wiechers