PS: File/Folder Copying in PowerShell

Many times we require to copy a few bytes of single file to a few GBs of DB files on the other.  Here are basic PS cmdlets that help you script/automate the tasks safely in PS.

Important Notes on Copy Behavior:

  1. Copy process merges the source files with destination path
  2. You need to use forceful copy if the destination folder already exists
  3. If destination folder doesn’t exist, creates destination folder with files under sources folder instead of copying the whole folder as is
  4. If destination folder exists, copy process copies the whole source folder as subfolder in the destination folder
  5. If the source files have special attributes like Read Only or of alias type, the copy process throws an access denied error when it has to overwrite the files on destination

Below are the various cases that you may come across with copying automation:

Copy File:

PS C:TEMP> Test-Path -Path C:tempDstFldr

PS C:TEMP> True
PS C:TEMP>
Copy-Item .SrcFldrTest.docx .DstFldr
PS C:TEMP> Get-ChildItem .DstFldr

    Directory: C:TEMPDstFldr

Mode                LastWriteTime     Length Name
—-                ————-     —— —-
-a—         3/17/2011   4:23 PM      15413 Test.docx

PS C:TEMP>

Copy File and Overwrite existing file in the destination path:

PS C:Temp>md5sum.exe SrcFldrTest.docx
e8372739b0380080143d217b668526de *Test.docx

PS C:Temp>md5sum.exe DstFldrTest.docx
46ce26194746bfd6beb5e08f3f7c93d4 *Test.docx

PS C:Temp> Copy-Item .SrcFldrTest.docx .DstFldrTest.docx
PS C:Temp>md5sum.exe SrcFldrTest.docx
e8372739b0380080143d217b668526de *Test.docx

PS C:Temp>

 

PS C:Temp> C:Toolsfciv.exe .SrcFldrTest.docx
//
// File Checksum Integrity Verifier version 2.05.
//
7614efb66096aa065778974efcb17ac8 .srcfldrtest.docx
PS C:Temp> C:Toolsfciv.exe .DstFldrTest.docx
//
// File Checksum Integrity Verifier version 2.05.
//
af900ca88e192ce18c8d4642669daa7d .dstfldrtest.docx
PS C:Temp> Copy-Item .SrcFldrTest.docx .DstFldrTest.docx
PS C:Temp> C:Toolsfciv.exe .DstFldrTest.docx
//
// File Checksum Integrity Verifier version 2.05.
//
7614efb66096aa065778974efcb17ac8 .dstfldrtest.docx
PS C:Temp>

 

Copy All Files in Folder:

PS C:TEMP> Test-Path -Path C:tempDstFldr

PS C:TEMP> True

PS C:TEMP> Copy-Item .SrcFldr*.* .DstFldr
PS C:TEMP> Get-ChildItem .DstFldr

    Directory: C:TEMPDstFldr

Mode                LastWriteTime     Length Name
—-                ————-     —— —-
-a—         3/17/2011   4:23 PM      15413 Test.docx

PS C:TEMP>

Copy All Files Matching Wildcards:

PS C:TEMP> Test-Path -Path C:tempDstFldr

PS C:TEMP> True

PS C:TEMP> Get-ChildItem .SrcFldrtest*.* | ft name

Name
—-
Test.bmp
Test.docx
Test.pl
Test.txt
test.vbs
Test2.bmp
Test2.docx
Test2.pl
Test2.txt
test2.vbs

PS C:TEMP> Copy-Item .SrcFldrtest*.?? .DstFldr

PS C:TEMP> Get-ChildItem .DstFldr | ft name

Name
—-
Test.pl
Test2.pl

PS C:TEMP>

Copy Folder including creation of destination folder with a different name:

If destination folder doesn’t exist, creates destination folder with files under sources folder instead of copying the whole folder as is including the subfolders and files

PS C:TEMP> Test-Path -Path C:tempCopiedFolder
False

PS C:TEMP> Copy-Item C:tempSrcFldr -Destination C:TEMPCopiedFolder -Recurse

PS C:TEMP> Get-ChildItem C:tempCopiedFolder | wc -l
19
PS C:TEMP>

Copy/Replicate Folder (retain the folder name as is):

If destination folder exists, copy process copies the whole source folder as subfolder in the destination folder

PS C:TEMP> Test-Path -Path C:TEMPnewlocation
True
PS C:TEMP> Get-ChildItem C:TEMPnewlocation
PS C:TEMP> Copy-Item C:tempSrcFldr -Destination C:TEMPnewlocation -Recurse
PS C:TEMP> Get-ChildItem C:TEMPnewlocation

    Directory: C:TEMPnewlocation

Mode                LastWriteTime     Length Name
—-                ————-     —— —-
d—-         5/10/2011  11:01 AM            SrcFldr

PS C:TEMP>

 

Compare two directories:

PS C:> $SrcFldr = Get-ChildItem -Path C:TEMPSrcFldr
PS C:> $IncompletedstFldr = Get-ChildItem -Path C:TEMPnewlocationSrcFldr
PS C:> Compare-Object -ReferenceObject $SrcFldr -DifferenceObject $IncompletedstFldr

InputObject                                                 SideIndicator
———–                                                 ————-
ExtraFile.pl                                                =>

PS C:>

Forcefully Copy/Replicate Folder Without Erasing or Retaining Extra Files in Existing Destination Folder:

Copy process merges the source files with destination path

PS C:> $SrcFldr = Get-ChildItem -Path C:TEMPSrcFldr
PS C:> $IncompletedstFldr = Get-ChildItem -Path C:TEMPnewlocationSrcFldr
PS C:> Compare-Object -ReferenceObject $SrcFldr -DifferenceObject $IncompletedstFldr

InputObject                                                 SideIndicator
———–                                                 ————-
ExtraFile1.pl                                               =>
Test.docx                                                   <=
Test.pl                                                     <=
Test.txt                                                    <=
test.vbs                                                    <=

PS C:> Copy-Item C:tempSrcFldr -Destination C:TEMPnewlocation -Recurse -Force
PS C:> $SrcFldr = Get-ChildItem -Path C:TEMPSrcFldr
PS C:> $IncompletedstFldr = Get-ChildItem -Path C:TEMPnewlocationSrcFldr
PS C:> Compare-Object -ReferenceObject $SrcFldr -DifferenceObject $IncompletedstFldr

InputObject                                                 SideIndicator
———–                                                 ————-
ExtraFile1.pl                                               =>

PS C:>

Forceful Copy:

You need to use forceful copy if the destination folder already exists .

If the source files have special attributes like Read Only or of alias type, the copy process throws an access denied error when it has to overwrite the files on destination.

PS C:> File C:TEMPSrcFldrtest2.vbs

Path: C:TEMPSrcFldrtest2.vbs
Attributes: R A
ID: 0x00060000 00033c81 on volume a4a0-8cdd (links 1)
Allocation: 0x1 clusters (starting at cluster 0x207e3) with 0 fragment(s)

PS C:> Get-ChildItem C:TEMPDstFldr
PS C:> Copy-Item C:tempSrcFldr*.* -Destination C:TEMPDstFldr -Recurse
PS C:> $SrcFldr = Get-ChildItem -Path C:TEMPSrcFldr
PS C:> $DstFldr = Get-ChildItem -Path C:TEMPDstFldr
PS C:>
Compare-Object -ReferenceObject $SrcFldr -DifferenceObject $DstFldr
PS C:> Copy-Item C:tempSrcFldr*.* -Destination C:TEMPDstFldr -Recurse
Copy-Item : Access to the path ‘C:TEMPDstFldrtest2.vbs’ is denied.
At line:1 char:10
+ Copy-Item <<<<  C:tempSrcFldr*.* -Destination C:TEMPDstFldr -Recurse
    + CategoryInfo          : PermissionDenied: (C:tempSrcFldrtest2.vbs:FileInfo) [Copy-Item], UnauthorizedAccessEx
   ception
    + FullyQualifiedErrorId : CopyFileInfoItemUnauthorizedAccessError,Microsoft.PowerShell.Commands.CopyItemCommand

PS C:> Copy-Item C:tempSrcFldr*.* -Destination C:TEMPDstFldr -Recurse -Force
PS C:>

Silence Copy Process Runtime Errors in Automated Scripts:

Many times copy process may end up in error condition due to syntax, folder struct
ure, permissions, in-use status, etc,. In such a case an automated copy script results in Error output causing script to end abruptly or await user responses.  To make you automation to proceed without worrying about the failures due error conditions.

Using the same example above:

PS C:> Copy-Item C:tempSrcFldr*.* -Destination C:TEMPDstFldr -Recurse
Copy-Item : Access to the path ‘C:TEMPDstFldrtest2.vbs’ is denied.
At line:1 char:10
+ Copy-Item <<<<  C:tempSrcFldr*.* -Destination C:TEMPDstFldr -Recurse
    + CategoryInfo          : PermissionDenied: (C:tempSrcFldrtest2.vbs:FileInfo) [Copy-Item], UnauthorizedAcce
   ception
    + FullyQualifiedErrorId : CopyFileInfoItemUnauthorizedAccessError,Microsoft.PowerShell.Commands.CopyItemComman

PS C:> Copy-Item C:tempSrcFldr*.* -Destination C:TEMPDstFldr -Recurse -ErrorAction  SilentlyContinue
PS C:>

 

Forceful Replace a folder:

$SourceFldr      = ‘C:tempSrcFldrFldr’
$DestParentFldr  = ‘C:TEMPDstFldr’

Remove-Item "$DestParentFldrFldr" -Force -Recurse -ErrorAction SilentlyContinue
Copy-Item $SourceFldr -Destination $DestParentFldr -Recurse -Force  -ErrorAction SilentlyContinue

 

Copy Folders along with the NTFS security permissions:

Very often when you copy over the systems files say C:ProgramData, etc., the permissions don’t match on the copied folder compared to the source folder permissions.  To fix this, you need to ensure you are copying the folder/files along with NTFS security permissions.   You don’t have a direct option to do copy along with security permission.  The tools available readily on Windows platforms for this is using xcopy or robocopy. 

Below is the example with XCOPY usage:

PS C:> $PROGDATA_DEST   = "C:ProgramData"
PS C:> Test-Path -path $PROGDATA_DESTSkype
False
PS C:> $PROGDATA_SRC = ‘C:tempskype’
PS C:> xcopy /O "$PROGDATA_SRC" "$PROGDATA_DEST" /Y /Q /I /E
Access denied
0 File(s) copied

PS C:> whoami
Teatlangovardhan
PS C:>

 

While updating system locations like "C:ProgramData", one needs to be an administrator or privileged user.

 

PS C:> whoami
TestsrvAdministrator

PS C:> $PROGDATA_DEST   = "C:ProgramData"
PS C:> Test-Path -path $PROGDATA_DESTSkype
False
PS C:> $PROGDATA_SRC = ‘C:tempskype’
PS C:> xcopy /O "$PROGDATA_SRC" "$PROGDATA_DEST" /Y /Q /I /E
88 File(s) copied
PS C:> @(gci "$PROGDATA_DEST" -recurse).count
98
PS C:> @(gci "$PROGDATA_DEST" -recurse|where {!$_.PsIsContainer}).count
88
PS C:>

0 thoughts on “PS: File/Folder Copying in PowerShell

  1. PS C:> Copy-Item C:tempSrcFldr*.* -Destination C:TEMPDstFldr -Recurse
    Copy-Item : Access to the path ‘C:TEMPDstFldrtest2.vbs’ is denied.
    At line:1 char:10
    + Copy-Item <<< Copy-Item C:tempSrcFldr*.* -Destination C:TEMPDstFldr -Recurse -Force

    THIS IS NOT WORKING!!!

  2. I was suggested this blog by my cousin. I’m now not certain whether this submit is written by him as nobody else realize such certain about my problem. You’re wonderful! Thank you!

  3. I want to Forcefully Copy/Replicate Folder Without Erasing Extra Files in Existing Destination Folder. What changes I have to make to the below code?

    Copy-Item -Force -Recurse –Verbose $releaseDirectory -Destination $sitePath

Leave a Reply

Your email address will not be published. Required fields are marked *