当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] 从 CMD 运行 PowerShell 脚本

作者:精品下载站 日期:2024-12-14 17:25:05 浏览:13 分类:玩电脑

从 CMD 运行 PowerShell 脚本


PowerShell 是一种多功能脚本语言,允许用户自动执行任务、管理系统和执行各种管理功能。它使用 PowerShell 终端来运行脚本。但是,您可以从 cmd(命令行)运行 PowerShell 脚本,以更轻松地自动执行任务。

从命令提示符 (cmd) 运行 PowerShell 脚本可以提供多种优势,例如脚本集成(批处理文件和 PowerShell 脚本)、自动化,最重要的是许多用户熟悉命令提示符。

在本文中,我们将讨论如何从 cmd 运行 PowerShell 脚本、从 cmd 执行命令并将参数传递给脚本。

先决条件

从 CMD 运行 PowerShell 脚本之前,请确保系统上已安装 PowerShell。您可以通过在 CMD 中运行以下命令来检查 PowerShell 的可用性和版本:

powershell -Command "Get-Host | Select-Object Version"

上述命令产生以下输出:

[玩转系统] 从 CMD 运行 PowerShell 脚本

从 CMD 运行 PowerShell 脚本

要从命令行运行PowerShell脚本,最简单的方法是直接执行脚本文件。

如果您的系统配置为默认运行 PowerShell 脚本,则此方法有效。

要直接从 cmd 运行脚本,请按照以下步骤操作:

  1. 启动命令提示符:使用“以管理员身份运行”打开命令提示符 (cmd)
  2. 键入PowerShell脚本路径:键入PowerShell脚本在控制台上的路径,例如... D:\PS\script1.ps1
  3. 按 Enter:按 Enter,它将在命令提示符控制台上执行 PowerShell 脚本。
  4. 检查脚本输出:如果脚本执行成功,它将在控制台上打印输出。
PS C:\> D:\PS\script1.ps1    

在上面的命令中,PowerShell 脚本存储在位置 D:\PS\script1.ps1

上述命令的输出将输出打印到控制台上。

PS C:\> D:\PS\script1.ps1                                                                                               
The example script program for demonstration.
PS C:\> 

使用 PowerShell.exe 从 CMD 运行 PowerShell 脚本

您可以使用 powershell.exe 从 cmd 运行 PowerShell 脚本。与直接执行脚本相比,此方法提供了更大的灵活性和对脚本执行的控制。

要使用 powershell.exe 从命令提示符执行 PowerShell 脚本,请使用以下语法:

PS C:\> powershell.exe "D:\PS\script1.ps1"  

上述命令的输出显示脚本结果如下:

[玩转系统] 从 CMD 运行 PowerShell 脚本

酷提示:如何在 PowerShell 中运行批处理文件!

使用 CMD 中的参数执行 PowerShell 脚本

如果 PowerShell 脚本需要参数,您可以在命令行上的脚本路径之后传递它们。

让我们考虑一下,您的 script1.ps1 文件包含以下需要一个参数的脚本。

Script1.ps1

param (
    [string] $Parameter1
    )
Write-Host "The parameters value pass from the cmd is: $parameter1"

要使用参数值从 cmd 运行脚本,请使用以下命令:

powershell -File "C:\path\to\your\script.ps1" -Parameter1 "Test"

上述命令的输出显示结果:

PS C:\> powershell.exe "D:\PS\script1.ps1" -Parameter1 "Test"                                                           The parameters value pass from the cmd is: Test
PS C:\>   

酷提示:如何使用 PowerShell 命令行参数!

从 CMD 运行 PowerShell 命令

您可以从 cmd 运行 PowerShell 命令,而无需创建脚本文件。

要执行 PowerShell 命令,请使用 -command 选项,后跟用双引号括起来的 PowerShell 命令。

PS C:\> powershell -Command "Get-Process"  

上面的命令从 CMD 运行 PowerShell 命令“Get-Process”并显示 Windows 系统中正在运行的进程。

常问问题

如何在 Windows 中使用命令提示符执行 PowerShell 脚本?

要在 Windows 中使用命令提示符执行 PowerShell 脚本,请使用以下命令:

powershell -File "C:\path\to\your\script.ps1"

从 CMD 运行 PowerShell 脚本有哪些不同的方法?

使用带有 -File-Command 参数的 powershell.exe 从 Windows 命令提示符运行脚本或直接从 cmd 执行 powershell 脚本。

从 CMD 运行 PowerShell 脚本时如何绕过执行策略?

要在从 CMD 运行 PowerShell 脚本时绕过执行策略,请使用 -ExecutionPolicy Bypass 参数:

powershell -ExecutionPolicy Bypass -File "D:\PS\script1.ps1" -Parameter1 "Test"

如何在从 CMD 运行脚本时隐藏 PowerShell 窗口?

要在从 cmd 运行脚本时抑制 PowerShell 窗口,请使用 -WindowStyle Hidden 参数:

powershell -WindowStyle Hidden -ExecutionPolicy Bypass -File "D:\PS\script1.ps1" -Parameter1 "Test"

结论

希望上面关于如何从 CMD 运行 PowerShell 脚本的文章对您有所帮助。从 Windows 命令提示符运行 PowerShell 脚本和命令可以灵活地将 PowerShell 功能集成到现有工作流程中。

您可以在 ShellGeek 主页上找到有关 PowerShell Active Directory 命令和 PowerShell 基础知识的更多主题。

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯