in

WindowsWindows Loving ItLoving It

PowerShell Copy Files with Copy-Item -TecKangaroo

Copy files form one location to another without much effort. You can copy and also rename the file in the same process. Copy multiple files with extension.

powershell copy

Copy-Item command let lets the programmer copy an item from one location to another.  This has very useful implications as we will see below.

Jump to:

People also search for:

PowerShell copy item using Copy-Item:

  • We will move the file text.txt to the folder named folder1.
text file
text file
  • We use the script below.

cpi .\text.txt .\folder1\text.txt

powershell copy
powershell copy
  • When we check the folder1 we can see the text file copied to it.
copied text file
copied text file

Copy and rename the file:

  • We can copy and rename the file in a single command using Powershell.
  • To do this, we will provide the new name in the destination path of the copy command.
  • The below steps shows this.
  • We check the contents of the current folder using the below script.

gci

contents of the current folder
contents of the current folder
  • The contents of the text.txt file can be seen using this script.

.\text.txt

view contents
view contents
  • The content of the file is hello world.
contents of the file
contents of the file
  • We can check the contents of the destination folder by providing the destination folder path as a parameter.

gci .\folder1\

check contents
check contents
  • We move the text.txt to folder1 and rename it to text1.txt using the below script.

cpi .\text.txt .\folder1\text1.txt

  • Checking the contents of folder1.

gci

check contents
check contents
  • Open the file using to confirm that it is the same file with a different name.

.\folder1\text1.txt

  • The content of the copied file is also hello world.
check the file contents
check the file contents

Copying multiple files with the same extension:

  • We have multiple text files in our current folder.
several text files
several text files
  • Copying all the files is possible using Wildcard as given below.

cpi ‘*.txt’ .\folder1\

wild card script
wild card script
  • We can check the content of the destination folder as given below.

gci .\folder1\

The output of the above script is shown in the image.

copied all the files
copied all the files

Verdict:

Copying files in PowerShell is a breeze.  We can easily use the cpi alias for copying files from one location to another. In this post, we learned several different cases of copying files.  Using these cases in the real file will take some time and practice.  We recommend you to go through the post thoroughly and if there is anything, please let us know in the comments below.

For more awesome posts, stay tuned to TecKangaroo.

Leave a Reply

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