2

Hi I have a script on a memory stick that I want to be able to run in cmd line before a computer has windows fully installed. (the stage just after you've connected to a network).

Cmd used to run the script.

Start > Run D:\pscript\Intune.ps1

This only opens a .txt file, while researching I've found that the reason this happens is due to security, is there anyway to override this bar changing the default file type out.

Vilestorm
  • 55
  • 7

1 Answers1

0

Unlike batch files (.cmd, .bat) associated with cmd.exe (the legacy Command Prompt), PowerShell's .ps1 script files are by default not directly executable from outside PowerShell.

Instead, they are treated as documents that are by default associated with either Notepad or the (obsolescent) Windows PowerShell ISE, depending on the , and invoking them therefore opens them for editing, which applies to the following contexts:

  • Invoking a .ps1 file from cmd.exe
  • Invoking a .ps1 file from Start Menu's Run dialog, as in your case (which you can invoke with WinKey+R, for instance)
  • Opening (double-clicking) a .ps1 file from File Explorer or Desktop.

To execute a .ps1 script from outside PowerShell, you must therefore invoke it via PowerShell's CLI, powershell.exe for Windows PowerShell, pwsh for PowerShell (Core) 7+.

In the simplest case, using Windows PowerShell and the -File parameter, as also shown by Mathias R. Jessen in a comment; see the comments below and the linked docs for additional parameters:

# Note:
#  * The effective execution policy applies; use -ExecutionPolicy Bypass to bypass.
#  * Profiles are loaded; use -NoProfile to suppress.
#  * The console window auto-closes when the script terminates; use -NoExit
#    to keep the session open.
powershell.exe -File D:\pscript\Intune.ps1

For a comprehensive overview of PowerShell's CLI, see this post.


It is possible - though not advisable - to configure your system to execute .ps1 files by default - see this answer.

mklement0
  • 382,024
  • 64
  • 607
  • 775