关于匹配:PowerShell脚本删除以” pict”开头,以” .dat”结尾且超过2年的文件不起作用

Powershell script that deletes files that start with “pict”, end with “.dat” and are older than 2 years not working

我当前正在尝试编写一个脚本,该脚本删除2岁以上的文件夹中所有文件的名称,例如" pictxxxx.dat "。它应在开头搜索" pict ",并在末尾搜索"。dat "。
这是我的代码:

1
2
3
4
5
6
7
8
9
    Get-ChildItem"C:\\Temp" | ForEach {
  $lastWrite = $_.LastWriteTime
  $timespan = new-timespan -days 730
  if (((get-date) - $lastWrite) -gt $timespan) {
    if ($_.Name -Match"pict\\d+(\\.dat)^") {
      Remove-Item $_ -WhatIf
    }
  }
}

这只是什么也没做。每当我运行该命令行时,该命令行将保持完全为空。删除WhatIf也无济于事。

任何和所有输入将不胜感激。


" pict \\\\\\\\ d(.dat)^ "应该为" ^ pict \\\\\\\\ d(.dat)"

1
2
3
4
5
6
7
8
9
10
11
12
$files = Get-ChildItem"C:\\Temp"
$comparetime = (get-date).AddDays(-730)
$matchstring ="^pict\\d+(\\.dat)"
foreach ($file in $files) {
    $lastWrite = $file.LastWriteTime
    $name = $file.name
    $fullname = $file.fullname
    if ($lastWrite -lt $comparetime -and $name -match $matchstring) {
        del $fullname
        Write-Host"del $fullname" -ForegroundColor Green
    }
}