Question:
It’s easy to copy multiple files to a folder that doesn’t exists and let it be created:
1 2 |
Copy-Item C:\Temp\aa C:\Temp2\DoesNotExist |
The command above will create the folder DoesNotExist. That’s what I’m after.
But what is the PowerShell syntax to the same when the source is only a single file?
1 2 |
Copy-Item C:\Temp\test.txt C:\Temp2\DoesNotExist |
I tried C:\Temp2\DoesNotExist\ (with the trailing slash), but Powershell says “The filename, directory name, or volume label syntax is incorrect.” and refuses to copy the single file.
Answer:
If you’re looking for a one liner solution, you can do this.
1 2 |
copy "C:\test2.txt" -Destination (New-Item "C:\Temp2\DoesNotExist\" -Type container -force) -Container -force |