PowerShell get-childitem cannot handle filename st

2019-08-02 09:51发布

问题:

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'