PowerShell 在命令提示符问题中,“Format-Table”未被识别为内部或外部命令、可操作程序或批处理文件

use*_*712 1 windows powershell cmd command-prompt

我正在 cmd 中执行 powershell 脚本。

首先我写命令

C:\Windows\system32>start powershell.exe Set-ExecutionPolicy RemoteSigned

它工作成功

比运行脚本我写命令

C:\Windows\system32>start powershell.exe C:\\Get-NetworkStatistics.ps1
Run Code Online (Sandbox Code Playgroud)

也能成功运行

问题是当我尝试运行该函数时

    C:\Windows\system32>start powershell.exe Get-NetworkStatistics -computername Gbsi1  | Format-Table -autosize
Run Code Online (Sandbox Code Playgroud)

它给出错误“‘Format-Table’未被识别为内部或外部命令、可操作程序或批处理文件。”

这是它的屏幕截图。 在此输入图像描述

在powershell中可以成功,但在cmd中却不能。管道有问题吗| 我把它放在格式表之前

Chr*_*n.K 5

正如您的情况一样,管道CMD不是由 powershell 解释的。因此,CMD将尝试执行名为 的命令Format-Table,该命令不存在(在 powershell 之外)。

您可以使用以下命令来逃避它^

start powershell.exe Get-NetworkStatistics -computername Gbsi1 ^| Format-Table -autosize
Run Code Online (Sandbox Code Playgroud)

或者通过引用完整的命令行

start powershell.exe "Get-NetworkStatistics -computername Gbsi1 | Format-Table -autosize"
Run Code Online (Sandbox Code Playgroud)

请注意,您的调用无论如何都是错误的,您需要-Command向 powershell 提供选项,如下所示:

start powershell.exe -Command "Get-NetworkStatistics -computername Gbsi1 | Format-Table -autosize"
Run Code Online (Sandbox Code Playgroud)

最后,你真的想用吗start?它将打开一个新窗口,该窗口将在命令完成后立即关闭。您还可以使用:

powershell.exe -Command "Get-NetworkStatistics -computername Gbsi1 | Format-Table -autosize"
Run Code Online (Sandbox Code Playgroud)