How to copy folders to specific folder in powershell? - powershell

I want to copy a folder which contain many file and other folders to a specific folder. However I get fail by using the following code.
Copy-Item "c:\F1" "c:\Fs\F1"
or
Copy-Item "c:\F1\*" "c:\Fs\F1"
The structure of folder1:
F1
F1-1
file B
F1-2
file C
file D
file A

You are probably missing the -recurse option.
Copy-Item -Path C:\F1 -Destination c:\FsF1 –Recurse

Related

PowerShell copy-item folder structure is NOT wanted

The folder named "z:\original" has hundreds of sub-folders containing multiple copies of the same .jpg files. I wanted to copy all .jpg files into a folder named "z:\dump" WITHOUT the folder structure and hopefully overwrite most of the copies. I used
copy-Item -path "Z:\original" -filter "*.jpg" -Destination "Z:\dump" -recurse -verbose
but this recreated the structure with the .jpg files. How can I dump all files into a single folder, using PowerShell?
Use combination of dir/Get-ChildItem and Copy-Item with pipeline.
$_.FullName - Full path to jpg file
$_.Name is file name only
Get-ChildItem Z:\original\*.jpg -Recurse | %{Copy-Item $_.FullName "Z:\dump\$($_.Name)"}
You might need to add -Force to overwrite same files

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: Why is recurse not working?

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

Excluding a file while using Copy-Item

I'm having a few issues while copying desktop files as a part of the back up process from local folder to server.
My script automatically creates a backup folder on the server and I just realised when it creates the back up folder windows automatically creates a desktop.ini file, and as desktop.ini is a system file the script failed to write another desktop.ini from desktop to the backup folder in server.
My initial code was :
Copy-Item ("$Global:localBackupRestoreLocation\Desktop\$file") ("$Global:remoteBackupRestorePath\$nameOfbackupDirectory") ($recursive="true")
But I was to use the exclude in that line and I know exclude does not work recursively and I have to use Get-ChildItem.
Try something like this
$Exclude = #('desktop.ini')
copy-item -Path $Source -Destination $Dest -Exclude $Exclude

In Powershell can you use the copy-item command to copy all the contents of a directory including folders?

I know how to do it with different commands but is there a way to make it so copy-item cmdlet also copies directories to the target folder for example if I wanted to copy everything in the C:\users folder including all directories and sub directories is it possible to do with the copy-item command.
You should be able to do it with:
copy-item C:\users -recurse
copy-item C:\users -destination c:\somewhereelse -recurse
The first way assumes you are currently in the destination folder, the second way is explicit.