PowerShell get-childitem cannot handle filename starting with [ character even with escape character

点点圈 提交于 2019-12-13 00:12:45

问题


I have a file in c:\temp directory named as [default].xml. When I issue command: get-childitem "c:\temp`[default].xml" in the powershell console, it returns me nothing. However if I issue this command: get-childitem "c:\temp*default].xml", it works as expected.

Why escape character on '[' is not working?


回答1:


See get-help about_wildcards

The [] are wildcard "globbing" operators. To use get-childitem on files that have those characters, use the -literalpath switch to disable wildcarding of the file spec:

get-childitem -literalpath "c:\temp[default].xml"

Alternatively, normal wildcarding still works in the .getfiles() method of a directoryinfo object:

(get-item c:\).getfiles("temp[default].*")



回答2:


You've got Microsoft official answer in an old Technet Windows PowerShell Tip of the Week.

You can use :

Get-ChildItem 'c:\temp\``[default``].xml'


来源:https://stackoverflow.com/questions/9508375/powershell-get-childitem-cannot-handle-filename-starting-with-character-even-w

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