Quantcast
Channel: TechNet Blogs
Viewing all articles
Browse latest Browse all 36188

Powershell Basics: Prompt To Copy File If It Does Not Exist

$
0
0

The PowerShell command Copy-Item will overwrite a file if it exists by default. This is unless that file is marked Read Only in which case you can use the -Force switch to overwrite the file.

What if you want to only copy the file if it doesn’t exist? Here’s a quick PowerShell script that will complete this task:

$filefrom = 'c:tempsomething.txt'
$fileto = 'c:temp1something.txt'
if (-not (test-path $fileto))
  {
    $opts = @{'path' = $filefrom; 'destination' = $fileto; 'confirm' = $false}
    copy-item @opts
  }
else
  {
    $opts = @{'path' = $filefrom; 'destination' = $fileto; 'confirm' = $true}
    copy-item @opts
  }

PowerShell_Tips_Tricks_thumb.png


Viewing all articles
Browse latest Browse all 36188

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>