PowerShell脚本删除指定日期前旧备份的语法错误排查求助
解决PowerShell删除旧备份脚本的语法错误
问题描述
需要编写PowerShell脚本删除指定日期(当前日期往前推60天)之前的旧备份文件,文件夹每周生成备份。原脚本尝试在foreach循环中同时提取文件的LastWriteTime和Name属性以筛选旧文件,但出现语法错误,无法正常执行。
原脚本代码
## Path $path = "C:\SFTP_Root\SMGR" ## Get the current date $Cur_dte = Get-Date echo "Today is $Cur_dte" ## Old date (Subtract current date by 2 months i.e. 60 days) $Old_dte = (Get-Date).AddDays(-60) echo "Old date is $Old_dte" ## Loop to extract the date and name of the backup) foreach (($i in Get-ChildItem -Path $path | select -ExpandProperty LastWriteTime) -and ($file in Get-ChildItem -Path $path | select -ExpandProperty Name)) { if ($i -lt $Old_dte) { echo "$file is old; will be deleted" } elseif ($i -gt $Old_dte) { echo "$file is recent; won't be deleted" } }
报错信息
At C:\Temp\backups_clean.ps1:14 char:10 + foreach (($i in Get-ChildItem -Path $path | select -ExpandProperty La ... + ~ Missing variable name after foreach. At C:\Temp\backups_clean.ps1:14 char:14 + foreach (($i in Get-ChildItem -Path $path | select -ExpandProperty La ... + ~~ Unexpected token 'in' in expression or statement. At C:\Temp\backups_clean.ps1:14 char:13 + foreach (($i in Get-ChildItem -Path $path | select -ExpandProperty La ... + ~ Missing closing ')' in expression. At C:\Temp\backups_clean.ps1:14 char:81 + ... ChildItem -Path $path | select -ExpandProperty LastWriteTime) -and ($ ... + ~ Unexpected token ')' in expression or statement. At C:\Temp\backups_clean.ps1:14 char:95 + ... $path | select -ExpandProperty LastWriteTime) -and ($file in Get-Chi ... + ~~ Unexpected token 'in' in expression or statement. At C:\Temp\backups_clean.ps1:14 char:94 + ... ath $path | select -ExpandProperty LastWriteTime) -and ($file in Get- ... + ~ Missing closing ')' in expression. At C:\Temp\backups_clean.ps1:14 char:153 + ... d ($file in Get-ChildItem -Path $path | select -ExpandProperty Name)) + ~ Unexpected token ')' in expression or statement. At C:\Temp\backups_clean.ps1:14 char:154 + ... d ($file in Get-ChildItem -Path $path | select -ExpandProperty Name)) + ~ Unexpected token ')' in expression or statement. + CategoryInfo : ParserError: (:) [], ParseException + FullyQualifiedErrorId : MissingVariableNameAfterForeach
修复方案
错误原因
PowerShell的foreach语法不支持同时遍历两个独立集合,原脚本写法不符合语法规范。更关键的是,分开提取LastWriteTime和Name会导致两个列表无法保证对应关系(比如文件顺序变化时日期和文件名错位)。
修正后的脚本
直接遍历Get-ChildItem返回的文件对象,每个对象本身包含LastWriteTime、Name、FullName等所有属性,无需拆分提取:
## Path $path = "C:\SFTP_Root\SMGR" ## Get the current date $Cur_dte = Get-Date Write-Host "Today is $Cur_dte" ## Old date (Subtract current date by 60 days) $Old_dte = (Get-Date).AddDays(-60) Write-Host "Old date is $Old_dte" ## Loop through each file in the target path foreach ($file in Get-ChildItem -Path $path) { if ($file.LastWriteTime -lt $Old_dte) { Write-Host "$($file.Name) is old; will be deleted" # 实际执行删除时,移除下面的注释,-WhatIf用于预演删除操作,确认无误后再去掉 # Remove-Item -Path $file.FullName -Force -WhatIf } else { Write-Host "$($file.Name) is recent; won't be deleted" } }
关键改进点
- 直接遍历文件对象,确保日期与文件名一一对应
- 使用
Write-Host替代echo(PowerShell中echo是Write-Output的别名,Write-Host更适合控制台输出信息) - 加入了带
-WhatIf参数的删除命令,可先测试删除范围,避免误删 - 简化了条件判断,无需单独处理
-gt分支,用else覆盖即可
内容的提问来源于stack exchange,提问作者Melwyn John
相关产品推荐
相关产品推荐

