user8570495
user8570495

Reputation: 1835

how to exec ps1 file from cmd?

I have a ps1 file, Test.ps1, which I need to exec from cmd. For test purposes this file only has 1 line:

write "ps1 test successful"

I was trying to exec this ps1 file from cmd. I googled and it seemed that including the following line might help:

Set-ExecutionPolicy RemoteSigned
write "ps1 test successful"

However I still can't exec this test. I've tried:

powershell Test
powershell Test.ps1
Test
Test.ps1

The cmd path context is set to the dir in which the ps1 script resides. Any idea what I might be doing wrong here?

Upvotes: 3

Views: 10469

Answers (4)

Luke
Luke

Reputation: 13

My PowerShell script (Test.ps1):

echo "trying to test something"

I can execute it in cmd with this command:

.\Test.ps1

My output:

trying to test something

Upvotes: -1

james.garriss
james.garriss

Reputation: 13406

On macOS:

  1. Use Homebrew to install Powershell:

    brew install --cask powershell

  2. Switch to Powershell:

    pwsh

  3. Execute the script:

    ./Test.ps1

Upvotes: 0

Bill_Stewart
Bill_Stewart

Reputation: 24585

Use

powershell.exe -ExecutionPolicy Bypass -File "C:\dir name\test.ps1"

Of course, replace C:\dir name\test.ps1 with the path and filename of the script you want to run, enclosed in " (double quotes).

Alternatively, start PowerShell in its own window, then run the script.

Upvotes: 3

Michael Armes
Michael Armes

Reputation: 1106

Does this work?

Powershell -ExecutionPolicy Bypass -File .\Test.ps1

I've done this before with a .bat file, and this was the syntax used. In this instance, you're running from within the same directory as the powershell script (otherwise adjust the filename argument as necessary). And you may need to be running the CMD prompt as admin, if you aren't already.

Upvotes: 4

Related Questions