Powershell - Copy file to another folder while keeping folder structure

I have a large folder with many sub directories and files. I am only interested to copy specific files from that large folder to a new folder along with the folder structure. I am using Powershell to achieve this goal. Right now I am interested to copy single file with folder structure. I will use a loop later on top of it once its done.

I can achieve this via coding in python or java but I have to run it on a remote server where I only have access to Powershell. I don't have much experience with Powershell.

I tried searching docs and internet and came up with the following two commands :

1) Copy-Item -Path LargeFolder\folder1\sub-folder1\file1.txt -Destination NewFolder\ -Recurse
2) Get-ChildItem -Path LargeFolder\folder1\sub-folder1\file1.txt | Copy-Item -Destination NewFolder\ -Recurse -Container

Both command copies only the file1.txt to the NewFolder without folder structure.

Output of both commands :

NewFolder |- file1.txt

Expected Output :

NewFolder |-folder1 |-sub-folder1 |-file1.txt
//or even this I don't mind an extra root folder
NewFolder |-LargeFolder |-folder1 |-sub-folder1 |-file1.txt

I am a bit lost here. How can I also retain the folder structure ?

6

2 Answers

You have to create the subfolder structure before you can copy the file to it:

$Destination = 'NewFolder'
$FileToCopy = 'LargeFolder\folder1\sub-folder1\file1.txt'
Get-Item $fileToCopy | Copy-Item -Destination { $DestPath = Join-Path $Destination (( $_.DirectoryName -Split 'LargeFolder' )[-1] ) If ( ! ( Test-Path )) { mkdir $DestPath | out-null } $DestPath
}

You can do this with Robocopy (with or without PowerShell involved at all) and or Copy-Item out of the box and all you need is in the help files:

'PowerShell using robocopy'

Youtube - 'PowerShell copy-item'

Robocopy /?
------------------------------------------------------------------------------- ROBOCOPY :: Robust File Copy for Windows
------------------------------------------------------------------------------- Started : Wednesday, 26 August, 2020 08:40:06 Usage :: ROBOCOPY source destination [file [file]...] [options] source :: Source Directory (drive:\path or \\server\share\path). destination :: Destination Dir (drive:\path or \\server\share\path). file :: File(s) to copy (names/wildcards: default is "*.*").
::
:: Copy options :
:: /S :: copy Subdirectories, but not empty ones. /E :: copy subdirectories, including Empty ones. /LEV:n :: only copy the top n LEVels of the source directory tree. /Z :: copy files in restartable mode. /B :: copy files in Backup mode. /ZB :: use restartable mode; if access denied use Backup mode. /J :: copy using unbuffered I/O (recommended for large files). /EFSRAW :: copy all encrypted files in EFS RAW mode. /COPY:copyflag[s] :: what to COPY for files (default is /COPY:DAT). (copyflags : D=Data, A=Attributes, T=Timestamps, X=Skip alt data streams). (S=Security=NTFS ACLs, O=Owner info, U=aUditing info). /SEC :: copy files with SECurity (equivalent to /COPY:DATS). /COPYALL :: COPY ALL file info (equivalent to /COPY:DATSOU). /NOCOPY :: COPY NO file info (useful with /PURGE). /SECFIX :: FIX file SECurity on all files, even skipped files. /TIMFIX :: FIX file TIMes on all files, even skipped files. /PURGE :: delete dest files/dirs that no longer exist in source. /MIR :: MIRror a directory tree (equivalent to /E plus /PURGE). ...

Youtube 'PowerShell copy-item'

# Get specifics for a module, cmdlet, or function
(Get-Command -Name Copy-Item).Parameters
(Get-Command -Name Copy-Item).Parameters.Keys
# Results
<#
Path
LiteralPath
Destination
Container
Force
Filter
Include
Exclude
Recurse
PassThru
Credential
Verbose
Debug
ErrorAction
WarningAction
InformationAction
ErrorVariable
WarningVariable
InformationVariable
OutVariable
OutBuffer
PipelineVariable
WhatIf
Confirm
UseTransaction
FromSession
ToSession
#>
Get-help -Name Copy-Item -Examples
# Results
<#
Copy-Item "C:\Wabash\Logfiles\mar1604.log.txt" -Destination "C:\Presentation"
Copy-Item "C:\Logfiles" -Destination "C:\Drawings" -Recurse
Copy-Item C:\Logfiles -Destination C:\Drawings\Logs -Recurse
Copy-Item "\\Server01\Share\Get-Widget.ps1" -Destination "\\Server12\ScriptArchive\Get-Widget.ps1.txt"
$Session = New-PSSession -ComputerName "Server01" -Credential "Contoso\PattiFul" Copy-Item "D:\Folder001\test.log" -Destination "C:\Folder001_Copy\" -ToSession $Session
$Session = New-PSSession -ComputerName "Server02" -Credential "Contoso\PattiFul" Copy-Item "D:\Folder002\" -Destination "C:\Folder002_Copy\" -ToSession $Session
$Session = New-PSSession -ComputerName "Server04" -Credential "Contoso\PattiFul" Copy-Item "D:\Folder003\" -Destination "C:\Folder003_Copy\" -ToSession $Session -Recurse
$Session = New-PSSession -ComputerName "Server04" -Credential "Contoso\PattiFul" Copy-Item "D:\Folder004\scriptingexample.ps1" -Destination "C:\Folder004_Copy\scriptingexample_copy.ps1" -ToSession $Session
$Session = New-PSSession -ComputerName "Server01" -Credential "Contoso\PattiFul" Copy-Item "C:\MyRemoteData\test.log" -Destination "D:\MyLocalData\" -FromSession $Session
$Session = New-PSSession -ComputerName "Server01" -Credential "Contoso\PattiFul" Copy-Item "C:\MyRemoteData\scripts" -Destination "D:\MyLocalData\" -FromSession $Session
$Session = New-PSSession -ComputerName "Server01" -Credential "Contoso\PattiFul" Copy-Item "C:\MyRemoteData\scripts" -Destination "D:\MyLocalData\scripts" -FromSession $Session
#>
Get-help -Name Copy-Item -Full
Get-help -Name Copy-Item -Online

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like