如何从命令提示符处运行 PowerShell 脚本?
要从命令提示符处运行 PowerShell 脚本,我们可以使用下面的命令。
示例
例如,我们有一个脚本 TestPS.ps1 先启动后台处理程序服务,然后将文件复制到另外一个位置。我们需要使用命令提示符来调用此脚本。
C:\> PowerShell.exe -command "C:\temp\TestPS.ps1"
上述命令类似于运行单个 PowerShell 命令。此处我们提供了脚本路径。
输出
C:\>PowerShell.exe -command "C:\temp\TestPS.ps1" VERBOSE: Performing the operation "Start-Service" on target "Print Spooler (Spooler)". Status Name DisplayName ------ ----- ---------- Running Spooler Print Spooler VERBOSE: Performing the operation "Copy File" on target "Item: C:\Temp\EnvVariable.txt Destination: C:\Temp\Test\EnvVariable.txt".
您也可以使用下面的命令,
C:\> PowerShell.exe Invoke-Command -ScriptBlock { "C:\temp\TestPS.ps1"}当您使用第三方工具或远程调用此命令时,本地计算机可能会阻止脚本执行。在这种情况下,您需要通过设置执行策略来启用脚本执行。
PowerShell.exe -ExecutionPolicy Unrestricted -command "C:\temp\TestPS.ps1"
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
安卓
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP