删除temp中的文件夹列表

时间:2015-03-29 23:04:35

标签: powershell

我想删除temp中的文件夹列表。 文件夹名称为数字 - 2,3,4等..

PS C:\Users\sos$> Get-ChildItem -path C:\Temp

是否有PowerShell方式来获取此脚本

2 个答案:

答案 0 :(得分:1)

Soheil Hashemi在回答这个问题时打开了包,所以让我们来看一个基本选项。

您需要拥有temp目录中只是一个数字的所有文件。让我们使用Where-Object来匹配我们要查找的文件。

$Path = "C:\temp"
Get-ChildItem $Path -Directory | Where-Object{$_.BaseName -notmatch "\D"} | Remove-Item -Confirm:$false -WhatIf

$path获取所有文件,并匹配文件名(不带扩展名)仅包含数字的所有文件。然后将结果传递到Remove-Item。当您确定找到正确的文件时,请删除-WhatIf

如果您没有PowerShell 3.0,则可以更改Where子句并删除-Directory开关。

Get-ChildItem $Path | Where-Object{$_.BaseName -notmatch "\D" -and $_.PSIsContainer} | Remove-Item -Confirm:$false -WhatIf

答案 1 :(得分:0)


你的问题不完整,但首先创建像我这样的列表

$a = @'
1.txt
2.txt
3.txt
4.txt
'@

$a = (get-content c:\filelist.txt)
那么

$a | foreach { Remove-Item c:\temp\$_ }