windows 如何运行 PowerShell 脚本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2035193/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 07:01:57  来源:igfitidea点击:

How to run a PowerShell script

windowspowershellscripting

提问by Pekka

How do I run a PowerShell script?

如何运行 PowerShell 脚本?

  • I have a script named myscript.ps1
  • I have all the necessary frameworks installed
  • I set that execution policything
  • I have followed the instructions on this MSDN help pageand am trying to run it like so: powershell.exe 'C:\my_path\yada_yada\run_import_script.ps1'(with or without --noexit)
  • 我有一个名为 myscript.ps1 的脚本
  • 我已经安装了所有必要的框架
  • 我设置执行政策的事情
  • 我已按照此 MSDN 帮助页面上的说明进行操作, 并尝试像这样运行它:( powershell.exe 'C:\my_path\yada_yada\run_import_script.ps1'有或没有--noexit

which returns exactly nothing, except that the file name is output.

除了输出文件名之外,它什么都不返回。

No error, no message, nothing. Oh, when I add -noexit, the same thing happens, but I remain within PowerShell and have to exit manually.

没有错误,没有消息,什么都没有。哦,当我添加时-noexit,同样的事情发生了,但我仍然在 PowerShell 中并且必须手动退出。

The .ps1 file is supposed to run a program and return the error level dependent on that program's output. But I'm quite sure I'm not even getting there yet.

.ps1 文件应该运行一个程序并根据该程序的输出返回错误级别。但我很确定我还没有到达那里。

What am I doing wrong?

我究竟做错了什么?

回答by marc_s

  1. Launch Windows PowerShell, and wait a moment for the PScommand prompt to appear
  2. Navigate to the directory where the script lives

    PS> cd C:\my_path\yada_yada\ (enter)
    
  3. Execute the script:

    PS> .\run_import_script.ps1 (enter)
    
  1. 启动 Windows PowerShell,等待PS命令提示符出现
  2. 导航到脚本所在的目录

    PS> cd C:\my_path\yada_yada\ (enter)
    
  3. 执行脚本:

    PS> .\run_import_script.ps1 (enter)
    

What am I missing??

我错过了什么??

Or: you can run the PowerShell script from cmd.exelike this:

或者:您可以cmd.exe像这样运行 PowerShell 脚本:

powershell -noexit "& ""C:\my_path\yada_yada\run_import_script.ps1""" (enter)

according to this blog post here

根据这里的这篇博文

Or you could even run your PowerShell script from your C# application :-)

或者您甚至可以从 C# 应用程序运行 PowerShell 脚本 :-)

Asynchronously execute PowerShell scripts from your C# application

从 C# 应用程序异步执行 PowerShell 脚本

回答by Keith Hill

If you are on PowerShell 2.0, use PowerShell.exe's -Fileparameter to invoke a script from another environment, like cmd.exe. For example:

如果您使用的是 PowerShell 2.0,请使用 PowerShell.exe 的-File参数从另一个环境调用脚本,例如 cmd.exe。例如:

Powershell.exe -File C:\my_path\yada_yada\run_import_script.ps1

回答by Chingiz Musayev

If you want to run a script without modifying the default script execution policy, you can use the bypassswitch when launching Windows PowerShell.

如果您想在不修改默认脚本执行策略的情况下运行脚本,您可以在启动Windows PowerShell时使用绕过开关。

powershell [-noexit] -executionpolicy bypass -File <Filename>

回答by Sudheesh

Type:

类型:

powershell -executionpolicy bypass -File .\Test.ps1

powershell -executionpolicy bypass -File .\Test.ps1

NOTE: Here Test.ps1is the PowerShell script.

注意:这Test.ps1是 PowerShell 脚本。

回答by Dennis

I've had the same problem, and I tried and tried... Finally I used:

我遇到了同样的问题,我试了又试……最后我用了:

powershell.exe -noexit "& 'c:\Data\ScheduledScripts\ShutdownVM.ps1'"

And put this line in a batch-file, and this works.

并将这一行放在一个批处理文件中,这是有效的。

回答by AndyM

If you only have PowerShell 1.0, this seems to do the trick well enough:

如果您只有PowerShell 1.0,这似乎可以很好地解决问题:

powershell -command - < c:\mypath\myscript.ps1

It pipes the script file to the PowerShell command line.

它将脚本文件通过管道传输到 PowerShell 命令行。

回答by electronictonic

Pretty easy. Right click the .ps1 file in Windows and in the shell menu click on Run with PowerShell.

挺容易。在 Windows 中右键单击 .ps1 文件,然后在 shell 菜单中单击Run with PowerShell

回答by Jose Evaristo Cepeda Barrera

An easy way is to use PowerShell ISE, open script, run and invoke your script, function...

一个简单的方法是使用PowerShell ISE,打开脚本,运行并调用您的脚本,函数...

Enter image description here

在此处输入图片说明

回答by Kiquenet

Using cmd (BAT) file:

使用 cmd (BAT) 文件:

@echo off
color 1F
echo.

C:\Windows\system32\WindowsPowerShell\v1.0\powershell.exe -ExecutionPolicy Bypass -File "PrepareEnvironment.ps1"

:EOF
echo Waiting seconds
timeout /t 10 /nobreak > NUL

If you need run as administrator:

如果您需要以管理员身份运行

  1. Make a shortcut pointed to the command prompt (I named it Administrative Command Prompt)
  2. Open the shortcut's properties and go to the Compatibility tab
  3. Under the Privilege Level section, make sure the checkbox next to "Run this program as an administrator" is checked
  1. 制作一个指向命令提示符的快捷方式(我将其命名为管理命令提示符)
  2. 打开快捷方式的属性并转到兼容性选项卡
  3. 在权限级别部分下,确保选中“以管理员身份运行此程序”旁边的复选框

回答by rainabba

If your script is named with the .ps1extension and you're in a PowerShell window, you just run ./myscript.ps1(assuming the file is in your working directory).

如果您的脚本以.ps1扩展名命名并且您在 PowerShell 窗口中,您只需运行./myscript.ps1(假设文件在您的工作目录中)。

This is true for me anyway on Windows 10 with PowerShell version 5.1 anyway, and I don't think I've done anything to make it possible.

无论如何,在使用 PowerShell 5.1 版的 Windows 10 上这对我来说都是正确的,而且我认为我没有做任何事情来使它成为可能。