简体   繁体   English

如何在启动powershell.exe之前设置首选项`$ ErrorView =“ CategoryView”`

[英]How set preference `$ErrorView = “CategoryView”` before start powershell.exe

How to set preference $ErrorView = "CategoryView" before start powershell.exe ? 如何在启动powershell.exe之前设置首选项$ErrorView = "CategoryView"

powershell.exe -command "$ErrorView = "CategoryView" ; dir wrong.txt" doesnt work. powershell.exe -command "$ErrorView = "CategoryView" ; dir wrong.txt"不起作用。

your code has a serious error in it. 您的代码中有严重错误。 you used 4 double quotes instead of two on the outside and a pair of single quotes on the inside. 您在外部使用了4个双引号而不是两个,在内部使用了一对单引号。 [ grin ] [ 咧嘴 ]

this works ... 这个工作...

powershell.exe -command  "$ErrorView = 'CategoryView' ; dir wrong.txt; pause"

remove the pause when you are certain things are working as needed. 当您确定某些东西正在按需运行时,请删除pause [ grin ] [ 咧嘴 ]

To complement Lee Dailey's helpful answer : As Lee points out, your primary problem is that you neglected to escape the " chars. embedded in your overall "..." command . 为了配合李戴利的有用的答案 :李指出,您的主要问题是,你忽视了逃跑"字符嵌入到你的整体。 "..."命令

Assuming that you're calling your command from outside of PowerShell , such as from cmd.exe (Command Prompt): 假设您从PowerShell 外部调用命令 ,例如从cmd.exe (命令提示符) 调用命令

  • Using embedded single -quoting ( '...' ) in lieu of the embedded "..." is an option in this case , as shown in Lee's answer, because CategoryView is to be treated as a literal string . 如Lee的回答所示, 在这种情况下 ,使用嵌入式引号( '...' )代替嵌入式"..."是一种选择 ,因为Lee的答案是这样,因为CategoryView被视为文字字符串
    Using ' for the embedded quoting conveniently obviates the need for escaping. '用作嵌入式引号可以方便地避免转义的需要。

  • However, in cases where the embedded string contains variable references (eg, $var ) or expressions (eg, $(Get-Date) ), use of a double-quoted string ( "..." ) is a must , because only double-quoted strings are expandable (interpolated). 但是,在嵌入式字符串包含变量引用 (例如$var )或表达式 (例如$(Get-Date) )的情况下,必须使用双引号字符串( "..." ,因为仅双引号字符串是可扩展的 (内插的)。 Escaping the embedded " as \\" is then a must . 然后必须转义嵌入的" as \\"

    • Note that, by contrast, inside PowerShell " chars. must be escaped as `" . 请注意,相比之下,PowerShell 内部的 "字符"必须转义为`"
# From cmd.exe, for instance.
C:\>powershell.exe -command  "$ErrorView = \"CategoryView\"; dir wrong.txt" 

If, for some reason, you must invoke another PowerShell instance from within PowerShell, use a script block ( { ... } ), which also obviates the need for escaping (and better integrates with the calling session by returning objects from the invocation, not just strings ). 如果由于某种原因必须 PowerShell中调用另一个PowerShell实例,请使用脚本块{ ... } ),该脚本块也无需转义(并通过从调用中返回对象来更好地与调用会话集成,不只是字符串 )。

# From Powershell.
PS> powershell.exe -command { $ErrorView = "CategoryView" ; dir wrong.txt }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 带参数的Start-Process powershell.exe - Start-Process powershell.exe with an argument Powershell.exe:关于语法的一般问题 - Powershell.exe: General Question about the Syntax 以编程方式获取powershell.exe的完整路径 - To get full path of powershell.exe programmatically powershell.exe-文件字符串数组作为参数 - powershell.exe -file string array as parameter 如何在新的 Windows 终端配置文件中使用 Anaconda 运行 powershell.exe? - How can I run powershell.exe with Anaconda in new windows terminal profiles? 在 Windows 终端上启动“powershell.exe”时如何修复错误 0x800700c1? - How to fix the error 0x800700c1 when launching `powershell.exe' on Windows Terminal? pipenv shell 在虚拟环境中启动子shell…“powershell.exe”不是内部或外部命令,也不是可运行的程序或 - pipenv shell Launching subshell in virtual environment… 'powershell.exe' is not recognized as an internal or external command, operable program or 升级到Windows 10后,批处理文件中的powershell.exe调用无法忍受 - powershell.exe call from batch file unbearable slow after upgrading to windows 10 安装nodejs后,Bamboo远程构建代理程序找不到powershell.exe - Bamboo remote build agent cannot find powershell.exe after installing nodejs 调用 vcvarsall.bat 在 Windows 7 / VS 2019 构建工具上给出“Windows 找不到 powershell.exe” - Calling vcvarsall.bat gives “Windows cannot find powershell.exe” on Windows 7 / VS 2019 Build tools
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM