Hi all,

I have the follwing script that goes at and hunts for files of a certain type on remote computers and stores them in a folder. It reads in PC names and username from a spreadsheet (.CSV).

I want to adapt it so that is only searches the user profile, not the entire C: drive.

So the line is:

$fileList = Get-WmiObject -Class CIM_DataFile -Filter "Drive='C:' And Extension='dic'" -Computername $computerName

I’d like to change is to that it has something like Path=C:\users$userName and searched that whole set of subfolders.

I can’t for the life of me figure out how to make it work. I keep getting invalid queries when I try to add a path.

Entire Script follows:

$computerName = @()
$userName = @()

Import-Csv C:\test\script\Computername_username_test.csv |`
	ForEach-Object {
			$computerName = $_.ComputerName
			$userName = $_.UserName

$fileList = Get-WmiObject -Class CIM_DataFile -Filter "Drive='C:' And Extension='dic' AND Path='c:\users\$userName'" -Computername $computerName
$destination = New-Item -ItemType Directory -Path C:\test\$userName\dictionary_Files\ -force      

    foreach ($file in $fileList)
    {
	    $drive, $path = $file.Name.Split('\',2)
	    $drive = $drive -replace ':','$'
        $remoteFile = "\\$computerName\$drive\$path"
        $FileName = ($path -split "\\")[-1] -replace ".dic"
        $destFileName = $FileName
        $count = 0
        While (Test-Path -Path "$destination\$destFileName.dic") {
            $count++
            $destFileName = "$FileName"+"$count"
        }
	    Write-Verbose "Copy $remoteFile to $destination\$destFileName.dic"
	    Copy-Item $remoteFile -Destination "$destination\$destFileName.dic" 
    }
}

Thank you all very much for any help you can provide!

@powershellman8045

3 Spice ups

You have to use double backslashes in the path due to it being the escape character. Also you have to use the ‘Like’ operator for the Path property instead of equals otherwise it will only look in that specific directory, not any subfolders

Get-WmiObject -Class CIM_DataFile -Filter "Drive='C:' And Extension='dic' And Path Like '%\\users\\$username\\%'"

Thank you for your response! I will try that.

that did it, thank you so much for your help!