PowerShell – Copy File
Note: This is a recovered post from my old blog. See how we recovered these posts using the Wayback Machine and AI tooling.
Copy a file from one location to another. The first version simply copies the file if the destination doesn’t exist. The second version also checks if the destination directory exists and creates it if needed before copying.
$File1 = "C:\test1\test.txt" # File you want to move
$File2 = "C:\test\test.txt" # Place to move file
If(!(test-path $file2))
{
Copy-Item -Path $File1 $File2
}
This version also checks if the destination directory exists and creates it if needed:
$dir = "C:\test" #Directory you want to make or check if exist
If(!(test-path $dir))
{
New-Item -ItemType Directory -Force -Path $dir
}
$File1 = "C:\test1\test.txt" # File you want to move
$File2 = "$dir\test.txt" # Place to move file
If(!(test-path $file2))
{
Copy-Item -Path $File1 $File2
}