The Copy-Item
cmdlet is used to copy files or folders from one location to another.
Copy-Item -Path "source file or folder" -Destination "destination path"
Path
: This is the source file path.Destination
: This is the destination file path.It does not return anything.
Let's take a look at an example of this:
#!/usr/bin/pwsh -CommandNew-Item -Path './test' -ItemType Directory > $null#displays files in current directoryWrite-Host "-------------Files in current directory-------------"Get-ChildItem -Name#copy fileCopy-Item "abc.txt" -Destination "./test/abc.txt"#displays files in test directoryWrite-Host "-------------Files in test directory after copy-------------"Get-ChildItem "./test" -Name
In the above code snippet:
test
in the current directory using the New-Item
cmdlet. We will copy the file to this folder.abc.txt
from the current directory to the test
directory using the Copy-Item
cmdlet.test
. As we can see, the file has been copied.