Search code examples
powershellcommand-linescriptingidempotent

Copy-Item no target directory


Say you have this folder

foo

You can run this command in Bash

cp -r --no-target-directory foo bar

and it will create

foo
bar # same contents as foo

Then you can re-run the command without changing the result. How can you do this with PowerShell?


Solution

  • So the problem is that

    cp -r -fo foo bar
    

    only works if bar does not exist and

    cp -r -fo foo/* bar
    

    only works if bar exists. So to work around, you need to make sure bar exists before doing anything

    md -f bar
    cp -r -fo foo/* bar