1

I want to make a file having Windows Powershell commands. Then I want to open it with windows powershell directly and without pressing any key I want windows powershell start running those commands directly same as command prompy I can make .cmd or .bat file.

For example: These are two commands or Powershell, I want to save this file. Then I want directly execute this file by powershell. I have tried to save it as ps1 and ps2 extension as well but not working. Many methods online are not working. Any solution?

enter image description here

8
  • powershell yourfilename.ps1 Type powershell /? from a command prompt for more information. Commented Nov 26, 2020 at 1:36
  • Do you mean by double clicking? Commented Nov 26, 2020 at 1:40
  • If I follow your method @KenWhite, I recieve bug in command prompt. My file name is Data.ps1. Error is The term 'Data.ps1' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. At line:1 char:1 + Data.ps1 + ~~~~~~~~ + CategoryInfo : ObjectNotFound: (Data.ps1:String) [], CommandNotFoundException + FullyQualifiedErrorId : CommandNotFoundException Commented Nov 26, 2020 at 3:27
  • and if I use command as powershell .\Data.ps1, Error is .\Data.ps1 : File D:\GuiGuiRenderer\Build\GuiGUiBuild\Data.ps1 cannot be loaded. The file D:\GuiGuiRenderer\Build\GuiGUiBuild\Data.ps1 is not digitally signed. You cannot run this script on the current system. For more information about running scripts and setting execution policy, see about_Execution_Policies at https:/go.microsoft.com/fwlink/?LinkID=135170. At line:1 char:1 + .\Data.ps1 + ~~~~~~~~~~ + CategoryInfo : SecurityError: (:) [], PSSecurityException + FullyQualifiedErrorId : UnauthorizedAccess Commented Nov 26, 2020 at 3:31
  • powershell data.ps1 will work fine. If you're getting different results, then edit your question to include exactly what you're running at the command line. If you're not running it from the command line, then include details of exactly how you're trying to run it, including any batch file code if relevant. Expecting us to read your mind to find out what the problem is won't work. Commented Nov 26, 2020 at 3:32

2 Answers 2

1
  • PowerShell script files, across all versions, use the .ps1 filename extension.

  • From within PowerShell, you can invoke them directly, e.g., .\script.ps1

    • Note that, unlike in cmd.exe, you must use .\ (or a full path) in order to execute a file located in the current directory - just script.ps1 won't work - see this answer for background information.
  • From cmd.exe, you must use PowerShell's CLI (powershell.exe in Windows PowerShell / pwsh in PowerShell [Core] v6+) in order to execute a script file:

    • powershell.exe -File script.ps1
    • pwsh -File script.ps1 (-File may be omitted)

Note that with -File the .\-prefix is not required.

However, if you use -Command (-c) instead (which is the default with powershell.exe, whereas pwsh now defaults to -File), you do need the .\, because the -Command argument(s) are interpreted as a piece of PowerShell code, i.e. as if you had submitted it inside a PowerShell session.

You've discovered this in your own answer, where you pass a PowerShell command directly to the (implied) -Command parameter.

Note, however, that it's better to double-quote such commands, so as to prevent cmd.exe from interpreting certain characters itself, which breaks the call.

For instance, the following call would break, if you didn't enclose the -Command (-c) argument in "...":

# From cmd.exe; "..." required.
C:\>powershell.exe -c "Write-Output 'a & b'"
a & b

Another important consideration is that you need to escape embedded " chars. as \" for the CLI (even though PowerShell-internally you would use `" or ""):

# From cmd.exe; note the inner " escaped as \"
C:\>powershell.exe -c "Write-Output \"hi there\""
hi there
Sign up to request clarification or add additional context in comments.

Comments

0

I have found the solution. I use command powershell.exe and can directly execute powershell commands within cmd. powershell.exe $MyVariable=Get-Content .\Path.txt is working fine for me

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.