I want to delete folders using powershell. here is the foldername
C:\File\Coastal [new]
C:\File_new\Coastal.[new]
C:\File\Russia`s.Book
C:\File_new\Russia`s Book
It looks like ' and [] making the problem. I tried following command:
Get-ChildItem "C:\File\" | Where{$_.Name -Match "Coastal[\s\.][new]"} | Remove-Item -recurse -Force -Verbose
Get-ChildItem "C:\File_new\" | Where{$_.Name -Match "Coastal[\s\.][new]"} | Remove-Item -recurse -Force -Verbose
Get-ChildItem "C:\File\" | Where{$_.Name -Match "Russia`s[\s\.]Book"} | Remove-Item -recurse -Force -Verbose
Get-ChildItem "C:\File_new\" | Where{$_.Name -Match "Russia`s[\s\.]Book"} | Remove-Item -recurse -Force -Verbose
In a regex , which is what the -match
operator operates on as its RHS, [
is a metacharacter , as evidenced by your use of character set [\s\.]
to match either a single whitespace character ( \s
) or a literal \.
(as an aside: .
doesn't need escaping inside [...]
).
Therefore, you must use \[
to escape those [
instances you want to be treated as literals , notably in the [new]
substring.
Alternatively - though less readably - you could use a character set to represent a literal [
: [[]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.