Copy one file to a nonexistent folder

It is easy to copy multiple files to a folder that does not exist and create it:

Copy-Item C:\Temp\aa C:\Temp2\DoesNotExist 

In the above command, the DoNotExist folder will be created. This is what I want.

But what is the PowerShell syntax for it when the source is only one file?

 Copy-Item C:\Temp\test.txt C:\Temp2\DoesNotExist 

I tried C: \ Temp2 \ DoesNotExist \ (with a trailing slash), but Powershell says, "The file name, directory name, or volume label syntax is incorrect." and refuses to copy one file.

+4
source share
1 answer

If you are looking for a single liner solution, you can do it.

 copy "C:\test2.txt" -Destination (New-Item "C:\Temp2\DoesNotExist\" -Type container -force) -Container -force 
+5
source

Source: https://habr.com/ru/post/1396660/


All Articles