How to Use Advance PowerShell Copy File and Folder
powershell copy file

How to Use Advance PowerShell Copy File and Folder

We all know it’s very easy to copy file and folder in Windows manually through GUI way. But PowerShell method play an important role when there are hundreds or thousands of files that need to copy.

In this post, we’ll discuss how to use PowerShell copy file and folder related complex task.

PowerShell Copy a File 

Let’s start firstly with normal copy a file task.

We’ll use Copy-Item command of PowerShell to copy a file to specific folder.

#For example we’re going to copy an ‘Encoding Time.csv‘ file to directory ‘C:\ProgramData\Resources\‘ in the below example:

Copy-Item -Path 'C:\temp\Encoding Time.csv' -Destination 'C:\ProgramData\Resources\'

You need to run Get-ChildItem command to see the contents of destination directory:

# Let's check the result of above command:
Get-ChildItem -Path 'C:\ProgramData\Resources'

    Directory: C:\ProgramData\Resources
Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a----       2020-05-13   7:16 PM         193003 Encoding Time.csv

PowerShell Copy Folder Only

Likewise, the above command if you try to copy a folder, therefore, you’ll face a small problem:

Copy-Item -Path 'C:\temp\script\Working' -Destination 'C:\ProgramData\Resources\' 

If you see the result by executing the below Get-ChildItem command. You’ll find that only the ‘Working’ directory has been copied without its inside contents:

# Let's check the result of above command:
PS C:\temp\script\Working> Get-ChildItem -Path 'C:\ProgramData\Resources' -Recurse

    Directory: C:\ProgramData\Resources

Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d-----       2021-05-03  12:15 AM                Working

If you need to copy only an empty directory then it’s fine however if you require to copy with inside contents and sub-directory then use the below commands.

PowerShell to Copy Folder with Subfolders

Now let’s use the asterisk ( * ) wildcard character in the below command to copy inside contents and sub-folders:

Copy-Item -Path 'C:\temp\script\Working\*' -Destination 'C:\ProgramData\Resources\'

For example, you’ll be able to see the below as a result where only inside contents are getting copied without the ‘Workingfolder (root directory).

# Let's check the result of above command:
PS C:\temp\script\Working> Get-ChildItem -Path 'C:\ProgramData\Resources' -Recurse

    Directory: C:\ProgramData\Resources

Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d-----       2021-05-03  12:13 AM                WMIDiag
-a----       2021-05-02   6:56 PM            709 Advance Copy.ps1
-a----       2021-05-02  12:58 PM          15371 Using Advance PowerShell to Copy File and Folder.docx

    Directory: C:\ProgramData\Resources\WMIDiag

Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a----       2015-04-22   4:58 PM           9027 EULA.TXT
-a----       2015-04-23   4:32 PM         769024 WMIDiag.doc
-a----       2015-04-23   3:07 PM        6238714 WMIDiag.vbs
-a----       2012-01-26  10:57 AM         551424 WMIDiag.xls

PS C:\temp\script\Working>

You may also try below PowerShell Copy-Item command with -Recurse parameter to copy the root folder with sub-folders contents:

Copy-Item -Path 'C:\temp\script\Working' -Destination 'C:\ProgramData\Resources\' -Recurse
# Let's check the result of above command:
PS C:\temp\script\Working> Get-ChildItem -Path 'C:\ProgramData\Resources\Working\' -Recurse


    Directory: C:\ProgramData\Resources\Working

Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d-----       2021-05-02  11:48 PM                WMIDiag
-a----       2021-05-02   6:56 PM            709 Advance Copy.ps1
-a----       2021-05-02  12:58 PM          15371 Using Advance PowerShell to Copy File and Folder.docx

    Directory: C:\ProgramData\Resources\Working\WMIDiag


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a----       2015-04-22   4:58 PM           9027 EULA.TXT
-a----       2015-04-23   4:32 PM         769024 WMIDiag.doc
-a----       2015-04-23   3:07 PM        6238714 WMIDiag.vbs
-a----       2012-01-26  10:57 AM         551424 WMIDiag.xls

PowerShell Copy Folder and Overwrite Existing

By default, PowerShell doesn’t overwrite the existing folders at the destination with the same names. If you want to overwrite then you have to use the -Force parameter:

Copy-Item -Path 'C:\temp\script\Working' -Destination 'C:\ProgramData\Resources\' -Recurse -Force

By default, the PowerShell Copy-Item command doesn’t show any result if execution completed successfully.

So you can use -Passthrough parameter to show the result after the execution and -Verbose parameter to show information while execution.

~Point to remember

PowerShell Copy File To Network Share

You can use the below PowerShell copy a file from the local system to network UNC share path:

Copy-Item –Path 'C:\temp\script\Working\Advance Copy.ps1' –Destination '\\Techiwiz\IT\transfer.ps1'

Likewise the above command, we’re copying the ‘Advance Copy.ps1‘ file to a network shared path and also rename by giving another name (transfer.ps1) at the destination path.

You can reverse the copy files from a network share path to a local system:

Copy-Item –Path \\Techiwiz\IT\Team.txt –Destination 'C:\temp\script\Working\'

PowerShell Copy File To Remote Computer

If there is no shared folder on the destination system, in that case, you can use the admin share path (C$, Admin$, D$)

Copy-Item –Path 'C:\temp\script\Working\Advance Copy.ps1' –Destination '\\DCServer\C$\Temp\'

PowerShell Copy File to Remoting Session 

You can also use the PowerShell remoting session to copy file and folder to the remote system.

For example, we’ll use the below command to copy the Agenda.docx file from the local computer to path C:\Temp\ of remote computer SystemA:

$RemoteSession = New-PSSession -ComputerName "SystemA" -Credential "Techiwiz\TestUser1"

Copy-Item "C:\temp\script\Working\Agenda.docx" -Destination "C:\Temp\" -ToSession $RemoteSession

PowerShell Copy File from Remoting Session

For example, we’ll use the below command to copy the Meeting.docx file from remote computer SystemB (path: C:\Source\Meeting.docx) to your local computer (Path: C:\temp\script\Working\):

$SourceSession = New-PSSession -ComputerName "SystemB" -Credential "Techiwiz\TestUser1"

Copy-Item "C:\Source\Meeting.docx" -Destination "C:\temp\script\Working\" -FromSession $SourceSession

PowerShell Copy File between Remoting Session

Similarly, you can also copy any file between two remote computers by using a remoting session.

$SourceSession = New-PSSession -ComputerName "SystemB" -Credential "Techiwiz\TestUser1"

$RemoteSession = New-PSSession -ComputerName "SystemA" -Credential "Techiwiz\TestUser1"

Copy-Item "C:\temp\script\Working\Agenda.docx" -Destination "C:\Temp\" -FromSession $sourcesession -ToSession $destsession -PassThru

$SourceSession, $RemoteSession | Remove-PSSession

For more training related resource, Please check this section

PowerShell Copy File to Multiple Computers

When you need to copy a file to multiple computers then you have to use ForEach loop. Which executes the copy command one by one on each computer mentioned in the command.

Let’s first store the computers name in an array variable ($Computers) and after that, we’ll use it in a ForEach loop:


#PowerShell Copy File to Multiple Computers
$Computers = 'ServerA', 'ServerB', 'ServerC', 'ServerD', 'ServerE', 'ServerF'

ForEach-Object ($computer -in $Computers)
{
    Copy-Item –Path 'C:\temp\script\Working\Advance Copy.ps1' –Destination "\\$computer\C$\Temp\"
}

Similarly you can use the above script for copy folders also by just mention folder name in place of file name.

PowerShell Copy item to All User Profiles

You must be aware that copy a file to all user’s profile is not exactly same like copy to any windows directory .

In that case, you need to store first all current user’s profile name in a variable. After that use ForEach loop to copy one by one to each user’s profile.

##Powershell Copy File to All User Profiles 

#Save all existising user's profile names in $AllUsers variable
$AllUsers = Get-ChildItem -Path 'C:\users\' | Select Name -ExpandProperty Name

#Use for each loop to copy file in all cureent users profile (AppData\Local\)
ForEach-Object ($User -in $AllUsers)
{
    Copy-Item -Path "C:\temp\Agenda.docx" -Destination "C:\users\$User\AppData\Local\Temp\" -Force
}

The file copied to the default user profile, which gets copied automatically to the new users profile on their first login.

~ Use of default user profile

You may also use below one-liner command to copy a file or Folder to each user’s profile.

#One-liner PowerShell command to copy a file to each user's profile
ForEach ($user in (Get-ChildItem 'C:\Users')) { Copy-Item "C:\temp\Agenda.docx" -Destination "C:\Users\$user\AppData\Local\Temp\" -Force}

Please also check this Microsoft official site for details information.

Conclusion

We’ve used different use case of Copy a file or folder through PowerShell command. It’s very useful to use PowerShell when there are lot of repeated jobs you need to do. It’s going to save a lot of time and efforts, so I recommended to use PowerShell as much as you can.

Leave a Reply