Powershell: Why is recurse not working? - powershell

Hi I have a script that should ideally copy an item and paste it onto a destination folder including all of it's sub folders. However I cannot get it to copy the item into the subfolders.
Here is the code:
Copy-Item "\\postowl\PEC Group\HR\Performance snapshot\Performance Snapshot Macro.xlsm" -Destination "\\postowl\PEC Group\HR\Performance snapshot\2017-2018" -Recurse -Force
Please help!

Recurse copies the subfolders and item from the source to the destination, preserving directory structure. It doesn't recurse in the destination and make multiple copies in the destination. You could use a ForEach-Object loop from the output of Get-ChildItem where you retrieve all of the folders you wish to copy to.
Get-ChildItem "\\postowl\PEC Group\HR\Performance snapshot\2017-2018" -Directory -Recurse | % {
Copy-Item "\\postowl\PEC Group\HR\Performance snapshot\Performance Snapshot Macro.xlsm" -Destination $_.fullname -Force }

Two things from my observation.
1)You are telling you are copying an item including its sub-folders, but in the source you have mentioned one file name with extension. not sure if thats the folder name in your case.
2) I found that in the destination you are not going inside the final level folder.
So put this and check:
Copy-Item "\\postowl\PEC Group\HR\Performance snapshot\*.xlsm" -Destination "\\postowl\PEC Group\HR\Performance snapshot\2017-2018\" -Recurse -Force
Note: If its a specific file, then you can remove the wildcard because that will copy all the files with the extension .xlsm . I have kept it because I am assuming you have only one file under that folder.
See the Documentation and the issue with PS:
Copy-Item Nature & Issue

Related

Verify file copied using copy-item

i'm trying to copy file from source to destination any verify if file copied or not.
But the problem is if i make changes inside the source file which was copied earlier then destination file not getting override when i execute the code again. Also i want log file each time files are copied.
Files in folder:- .csv, .log, .png, .html
$source="C:\52DWM93"
$destination="C:\Temp\"
Copy-Item -Path $source -Destination $destination -Force
$ver=(Get-ChildItem -file -path $destination -Recurse).FullName | foreach {get-filehash $_ -Algorithm md5}
If($ver)
{Write-Host "ALL file copied"}
else
{Write-Host "ALL file not copied"}
If you copy directories like this you need the -Recurse switch for Copy-Item. Without it you're not going to copy anything except the directory itself.
You can of course also use Get-ChildItem with whatever filter or Recurse flag you care about and pipe that into Copy-Item.
Use the *-FileCatalog cmdlets for verification.

moving items from a folder to another but within another subdirectory

I have the text files in one file (C:/test/ex135). I want to move these files onto a new destination but the ex135 is hidden inside another directory named bob. So I would want to move these text files to C:/test/bob/ex135. Can I have powershell search for "ex135" within certain file path and move items here? If I knew the destination file, I would simply do copy-item -path "C:/test/ex135" -Destination "C/test/bob/ex135" but the destination path is not clear (I would want powershell to find this destination).
Suppose all txt files to be transported are in c:\test\ex135
the target folder is ex135 but in an unknown subdirectory, somewhere deep down c:\test\bob
then
$find_ex135 = Get-ChildItem "C:\test\bob" -Recurse | Where-Object { $_.PSIsContainer -and $_.Name.StartsWith("ex135")}
Get-ChildItem -Path C:\test\*.txt | Move-Item -Destination $find_ex135.FullName

Need assistance copying task

I have .dll and .sys files under folder xyz
xyz
'QcXhciFilter8086\QcXhciFilter8086.sys'
'qSarMgr8086\qSarMgr.dll'
'qcwlan_wpextwapi8086\WapiIhvExt.dll'
These need to be copied something like this under new folder
new
'QcXhciFilter8086\QcXhciFilter8086.sys'
'qSarMgr8086\qSarMgr.dll'
'qcwlan_wpextwapi8086\WapiIhvExt.dll'
What i have tried:
'Copy-Item -Path $file_path\..\*sys -Destination C:\Users\Path\new\'
Here '$file_path = \xyz\QcXhciFilter8086\QcXhciFilter8086.sys'
Result: Only .sys.dll files getting copied directly under "new" folder. However i want them under the driver name. Something like this 'new\QcXhciFilter8086\QcXhciFilter8086.sys'
Okay, I think I understand what you are asking for...
You have a folder structure like this:
[xyz]
QcXhciFilter8086
QcXhciFilter8086.sys
qSarMgr8086
qSarMgr.dll
qcwlan_wpextwapi8086
WapiIhvExt.dll
And you want this copying to another folder. Using this will copy the folder structure and files from the xyz folder to the new folder. This will copy everything, however. If you only want to copy the files that are only .sys and .dll then you will need to add another Get-ChildItem in the existing ForeEach-Object loop to look at the current directory.
Here is my proposed solution:
$source = "c:\path\to\xyz"
$destination = "c:\path\to\new"
Get-ChildItem -Path $source -Directory | ForEach-Object{
Copy-Item $_.FullName -Destination $destination -Recurse -Container
}

Powershell Copy files and folders

I have a PS script which Zips up the previous months logs and names the zip file FILENAME-YYYY-MM.zip
This works
What I now want to do is copy these zip files off to a network share but keeping some of the folder structure. I currently a folder structure similar to the following;
C:\Folder1\
C:\Folder1\Folder2\
C:\Folder1\Folder3\
C:\Folder1\Folder4\Folder5\
There are .zip files in every folder below c:\Folder1
What I want is for the script to copy files from c:\folder1 to \\networkshare but keeping the folder structure, so I should have 3 folders and another subfolder in folder4.
Currently I can only get it to copy the whole structure so I get c:\folder1\... in my \\networkshare
I keep running into issues such as the new folder structure doesn't exist, I can't use the -recurse switch within the Get-ChildItem command etc...
The script I have so far is;
#This returns the date and formats it for you set value after AddMonths to set archive date -1 = last month
$LastWriteMonth = (Get-Date).AddMonths(-3).ToString('MM')
#Set destination for Zip Files
$DestinationLoc = "\\networkshare\LogArchive\$env:computername"
#Source files
$SourceFiles = Get-ChildItem C:\Sourcefiles\*.zip -Recurse | where-object {$_.lastwritetime.month -le $LastWriteMonth}
Copy-Item $SourceFiles -Destination $DestinationLoc\ZipFiles\
Remove-Item $SourceFiles
Sometimes, you just can't (easily) use a "pure PowerShell" solution. This is one of those times, and that's OK.
Robocopy will mirror directory structures, including any empty directories, and select your files (likely faster than a filter with get-childitem will). You can copy anything older than 90 days (about 3 months) like this:
robocopy C:\SourceFiles "\\networkshare\LogArchive\$($env:computername)\ZipFiles" /E /IS /MINAGE:90 *.zip
You can specify an actual date with /MINAGE too, if you have to be that precise.
How about Copy-Item "C:\SourceFiles\" -dest $DestinationLoc\ZipFiles -container -recurse? I have tested this and have found that it copies the folder structure intact. If you only need *.zip files, you first get them, then for each you call Resolve-Path with -Relative flag set and then add the resultant path into Destination parameter.
$oldloc=get-location
Set-Location "C:\SourceFiles\" # required for relative
$SourceFiles = Get-ChildItem C:\Sourcefiles\*.zip -Recurse | where-object {$_.lastwritetime.month -le $LastWriteMonth}
$SourceFiles | % {
$p=Resolve-Path $_.fullname -relative
copy-item $_ -destination "$DestinationLoc\ZipFiles\$p"
}
set-location $oldloc # return back

powershell: Copy files of type AND their path values

I need to copy all my fileserver psts to another location with their folder paths.
I did the following
Get-ChildItem D: -recurse -include *.pst | copy-item -destination z:\
But this resulted in copies of like filenames being created.
I need the copy to write out the path name (its home folders so the pathname will help with easy ownership)
Any suggestions?
You could use the -recurse and -force flag on copy-item. It might create the folder structure. If it doesn't, you can use New-item to create that structure with a bit of work. If you're looking for an answer like that (i.e. you want folder structure copied as well) update your question with more detail and you'll get an answer focused on that.
What I would do, if I was just looking for a unique name for each PST, is to convert the full name to a file name on Z:, like so:
$psts = Get-ChildItem D:\ -recurse -include "*.pst"
$psts | % {
Copy-Item $_.FullName -Destination ($_.FullName.Replace("D:\","Z:\").Replace("\","-"))
}
It might be messy, but it should be unique.