1

I peiced this together but am kind of stuck. The code is placing the items in the root instead of the folder it came from ie if it was in a test folder it is placing it directly in the user folder, I want it to create a test folder and put the item in there. After it has copied the items I would like it to zip up the location. At one point I was thinking I could create a txt file then use that to copy the items but then I got lost.

$PCName = Read-Host "Enter Machine Name"
$User = Read-Host "Enter User Name"
$NAS = "\\<nas>\users\$user"
$Dir = get-childitem \\$PCName\C$\users\$User -force -recurse | where {
$_.extension -match "doc|xls|docx|xlsx|pdf|pst|txt|jpg|tif"} | ForEach {
Copy-Item $_.FullName -Destination $NAS -force -ErrorAction:SilentlyContinue
}


#$Dir | ft fullname | out-file C:\Scripts\logs\FileList\$User.txt
$Dir | format-table name# PowerShell script to list the DLL files under the system32 folder

1 Answers1

0

You'll need to replace the root of the source directory with the root of the destination directory in the destination path:

$PCName = Read-Host "Enter Machine Name"
$User = Read-Host "Enter User Name"
$NAS = "\\<nas>\users\$user"
$sourceRoot = "\\$PCName\C$\users\$User"
$Dir = get-childitem $sourceRoot -force -recurse | 
    where { $_.extension -match "doc|xls|docx|xlsx|pdf|pst|txt|jpg|tif"} | 
    ForEach {  
        $destFile = $_.FullName.Replace($sourceRoot, $NAS)
        New-Item -ItemType File -Path $destFile -Force
        Copy-Item $_.FullName -Destination $destFile -force -ErrorAction:SilentlyContinue }
Aaron Jensen
  • 25,861
  • 15
  • 82
  • 91
  • I was receiving an error that part of the path didn't exist. I was able to use the touch method described here: http://stackoverflow.com/a/7523632/1515942. The combination of above and the link gave me this just above the copy-item line New-Item -ItemType File -Path $destFile -force – Jamie Fisher Jul 11 '12 at 12:18