Powershell 获取路径中的Childitem和方括号[]或:LiteralPath+;星号

Powershell 获取路径中的Childitem和方括号[]或:LiteralPath+;星号,powershell,powershell-2.0,Powershell,Powershell 2.0,我试图将通配符(*)与LiteralPath一起使用,这样我的代码也可以在带括号的路径上工作 目前,我的代码没有错误,但它也不会删除有问题的文件 $path=“f:\test\test[lala]lulu\file.rar” $remfile=$(get childitem-LiteralPath$path) $remove=$remfile.fullname.substring(0,$($remfile.fullname).length-3) get childitem$($remove+'*

我试图将通配符(*)与
LiteralPath
一起使用,这样我的代码也可以在带括号的路径上工作

目前,我的代码没有错误,但它也不会删除有问题的文件

$path=“f:\test\test[lala]lulu\file.rar”
$remfile=$(get childitem-LiteralPath$path)
$remove=$remfile.fullname.substring(0,$($remfile.fullname).length-3)
get childitem$($remove+'*')| remove item-force
通过
remove
行,我去掉了名称的最后3个字符。到目前为止,一切顺利:

write-output $remove
F:\test\test[lala] lulu\file.
最后一行在搜索字符串中添加了一个星号,它不会给我一个错误,但也不会删除任何内容

将另一个
LiteralPath
添加到最后一行的GCI会导致错误,因为星号未用作通配符


我怎样才能做到这一点呢?

这里有一个窍门-即使在PS 6中,筛选器也不将括号理解为通配符

get-childitem -filter file[*] | remove-item -whatif
编辑:哦,这是一个目录。那么:

 get-childitem -LiteralPath 'F:\test\test[lala] lulu\' -filter file* | remove-item -WhatIf
或者,如果您已经在F:驱动器上:

get-childitem -filter 'test\test[lala] lulu\file*' | remove-item -WhatIf
编辑:您还可以将以下内容带入其中:

get-childitem -LiteralPath 'test[lala] lulu' -filter file* | 
where name -notlike *.txt | remove-item -WhatIf

非常感谢你,js2010。这帮了大忙。我将重写我的代码,这样我就只有literalpath段中的路径,并对没有最后3个字符的文件使用过滤器。再次感谢。好吧,事情变得更复杂了。当过滤器处于活动状态时,我的“排除”语句似乎不再有效。在原文中,我使用了类似-exclude*.txt的excludes