Copying UNC path with PowerShell
If you copy a UNC path (like \\system\share\) to some folder with Copy-Item cmdlet you'll end up with the target path including the share name. And this is usually undesirable.
If you copy a UNC path (like \system\share) to some folder with the Copy-Item
cmdlet you'll end up with the target path including the share name. And this is usually undesirable.
For example, running Copy-Item \\system\share\ d:\backup\ -Recurse
will end up putting everything under d:\backup\share
. That's annoying and not what I would expect. This is also the case when doing a local system device, like \\.\CDROM1\
. There's an easy (ish) solution. Create a temporary drive using new-psdrive
, do the copy, then remove the temporary drive using remove-psdrive
. The complete command turns into:
New-PSDrive -Name source -Root \\.\CDROM0\ -PSProvider FileSystem
Copy-Item -Path source:\* -Destination c:\backup\ -Recurse -Verbose
Remove-PSDrive source
I needed this because I wanted to mount an ISO, recursively copy all the files to the root of a thumb drive, then unmount the ISO. I also didn't want to mount it to a drive letter and have it pop up the windows in Explorer.