使用PS扫描和删除特定文件

时间:2017-06-26 19:16:48

标签: powershell

我有一个用于升级的脚本。我们发现升级后有几个用户目录(Win7)中有一个ini文件。我们可以转到每台计算机并在高架cmd提示符下使用del /s "filename.ini"(显然没有引号)。这将是太多的工作,因为有800-900台PC。试图在脚本中添加一行来查找和删除这些文件,而不是工作....

PC名称在csv文件中,因此有一个变量指向该文件:

# Read "Computers.csv" to get list of computers to upgrade.

$Computers = Get-Content Computers.csv

# Execute the upgrade process on computers in "Computers.csv sequentely.

ForEach ($Computer in $Computers)

然后试图添加这一行:

foreach ($termreg in $termregs) {
    remove-item $termreg.fullname
}

文件位于:

$termreg = gci $UsersPath -Recurse | Where {$_.name -like "test.txt"}

我错过了什么?

1 个答案:

答案 0 :(得分:0)

很难确切地看到你的脚本出了什么问题,因为部件出现故障或丢失。这是一种方法:

#This isn't a properly formatted csv if you are using Get-Content rather than Import-CSV
$Computers = Get-Content Computers.csv
#Invoke-Command runs in parallel when provided multiple systems to run on
Invoke-Command -ComputerName $Computers -Scriptblock {
    #Use gwmi if not PS3+, can use -filter to exclude users
    $UserDirectories = Get-CimInstance Win32_UserProfile |
        Select-Object -ExpandProperty LocalPath
    foreach ($UserDirectory in $UserDirectories) {
        Get-ChildItem $UserDirectory -Recurse -Include "test.txt" |
            Remove-Item -Force
    }
}