How to copy a file to a windows directory

A file should be copied to a different windows directory and the directory should be created if it does not exist.

The Copy-Item commandlet can be used to copy files from a source directory to a target directory.

$sourceFile = "C:\<YourFolder>\<YourFile>"  
$targetDirectory = "C:\<YourFolder>"

#Test if path is exists
if(!(Test-Path $targetDirectory)){
mkdir $targetDirectory
}

Copy-Item -Path $sourceFile -Destination $targetDirectory
 
  • Define the source file and the target directory
  • Check if the target directory exists. If the target directory does not exist create it
  • Copy the source file to the target directory

Remarks