LiteralPath option for cmdlet

安稳与你 提交于 2020-01-24 08:40:07

问题


In most example that I see in tutorials and books, the -LiteralPath option is almost never used by default (the -Path option seems to be preferred). Because the -LiteralPath option allows to use reserved characters (e.g. []), I don't understand why it is not used more often (if not, all the time). Is it because it is preferred to escape reserved characters manually, because it has a high performance cost, because not all cmdlet support this option or because something else?


回答1:


One thing to consider is that -Path is most likely to be the parameter that used when a string is passed in through the pipeline.

Looking at the Get-Item cmdlet for instance:

-LiteralPath <string[]>

Required?                    true
Position?                    Named
Accept pipeline input?       true (ByPropertyName)
Parameter set name           LiteralPath
Aliases                      PSPath
Dynamic?                     false

-Path <string[]>

Required?                    true
Position?                    0
Accept pipeline input?       true (ByValue, ByPropertyName)
Parameter set name           Path
Aliases                      None
Dynamic?                     false

Piping a string gets you -Path. Your object(s) would have to have a LiteralPath (or PSPath) property for Get-Item to use it. If your object has both, -Path is used.

I guess this doesn't necessarily answer the question; it's a bit circular to say "-Path is used more often because it's more popular". A better question might be, "Why doesn't -Path behave like -LiteralPath in the first place?"



来源:https://stackoverflow.com/questions/28611307/literalpath-option-for-cmdlet

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!