如何在 PowerShell 上转义命令行参数? [英] How to escape command line arguments on PowerShell?

查看:64
本文介绍了如何在 PowerShell 上转义命令行参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

看来我可以使用单引号或双引号转义命令行参数:

PS C:\>回声你好世界你好世界PS C:\>回声'你好世界'你好,世界PS C:\>回声你好世界"你好,世界

但是还有一些我想不通的地方,那就是当您希望从包含空格的目录中运行可执行文件时:

PS C:\>c:\程序文件\test.exe术语c:\program"未被识别为 cmdlet、函数、可运行程序或脚本文件.验证术语并重试.在行:1 字符:11+ c:\program <<<<文件\test.exePS C:\>'c:\程序文件\test.exe'c:\程序文件\test.exePS C:\>c:\程序文件\test.exe"c:\程序文件\test.exePS C:\>

如何让 PowerShell 运行上面的可执行文件?

解决方案

使用这个:

<预><代码>.c:\程序文件\test.exe"

实际上更好的解决方案是:

Invoke-Item "c:\program files\test.exe"

或使用别名:

ii "c:\program files\test.exe"

使用 Invoke-Item 意味着将使用正确的 Windows 文件处理程序.所以对于 EXE 文件,它会运行它.例如,对于 .doc 文件,它会在 Microsoft Word 中打开它.

这是最方便的 PowerShell 命令行之一.试试看:

ii .

It appears that I can escape command line arguments using single or double quotes:

PS C:\> echo Hello World
Hello
World
PS C:\> echo 'Hello World'
Hello World
PS C:\> echo "Hello World"
Hello World

But there's still something I can't figure out, which is when you wish to run an executable from a directory that contains a space in it:

PS C:\> c:\program files\test.exe
The term 'c:\program' is not recognized as a cmdlet, function, operable program, or script file. Verify the term and try again.
At line:1 char:11
+ c:\program  <<<< files\test.exe
PS C:\> 'c:\program files\test.exe'
c:\program files\test.exe
PS C:\> "c:\program files\test.exe"
c:\program files\test.exe
PS C:\>

How do I get PowerShell to run the executable above?

解决方案

Use this:

. "c:\program files\test.exe"

Actually an even better solution would be:

Invoke-Item "c:\program files\test.exe"

or using the alias:

ii "c:\program files\test.exe"

Using Invoke-Item means that the proper Windows file handler would be used. So for an EXE file it would run it. For a .doc file for instance, it would open it in Microsoft Word.

Here is one of the handiest PowerShell command lines around. Give it a try:

ii .

这篇关于如何在 PowerShell 上转义命令行参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆