[英]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 \\"
。
"
chars. must be escaped as `"
. "
字符"
必须转义为`"
。 # 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.