Your coworker might be confusing PowerShell with VBScript. VBScript's CopyFile
and CopyFolder
methods require a trailing backslash if the destination is a folder. PowerShell's Copy-Item
cmdlet doesn't care whether the destination does or doesn't have a trailing backslash as long as the folder exists.
He's not entirely wrong, though. If the destination folder doesn't exist a trailing backslash in the destination path does make a difference:
PS C:\Temp> ls -r | select Mode, FullName | ft -AutoSize
Mode FullName
---- --------
d---- C:\Temp\a
-a--- C:\Temp\a\foo.txt
PS C:\Temp> Copy-Item 'C:\Temp\a\foo.txt' 'C:\temp\b'
PS C:\Temp> ls -r | select Mode, FullName | ft -AutoSize
Mode FullName
---- --------
d---- C:\Temp\a
-a--- C:\Temp\b
-a--- C:\Temp\a\foo.txt
PS C:\Temp> Remove-Item 'C:\temp\b'
PS C:\Temp> Copy-Item 'C:\Temp\a\foo.txt' 'C:\temp\b\'
Copy-Item : The filename, directory name, or volume label syntax is incorrect.
...
A non-existing destination path with a trailing backslash throws an error, whereas without a trailing backslash the destination is created as a file.
However, IMO a better way to deal with missing destination folders is to actually check for their presence and create them if they don't exist:
if (-not (Test-Path -LiteralPath $destination -Type Container)) {
New-Item -Type Directory -Path $destination | Out-Null
}