Omitted sensitive data.
I can't get PowerShell to connect to a shared folder.
Copy-Item : Cannot find path '\\192.168.0.000\Common Data\Software\Crystal Reports\CRRedist2005_X64.msi' because it does not exist.
This does work on one machine but not another.
$source = "\\192.168.0.000\Common Data\Software\Crystal Reports\CRRedist2005_X64.msi"
Copy-Item $source -Destination $env:USERPROFILE\Downloads -Verbose
This doesn't work on either machine
Copy-Item "\\192.168.0.000\Common Data\Software\Crystal Reports\CRRedist2005_X64.msi" -Destination $env:USERPROFILE\Downloads -Verbose
I've tried using the New-PSDrive method
New-PSDrive -Name S -Root "\\192.168.0.000\Common Data\" -PSProvider FileSystem
Copy-Item "S:\Software\Crystal Reports\CRRedist2005_X64.msi" -Destination $env:USERPROFILE\Downloads -Verbose
Gives The specified drive root "\\192.168.0.000\Common Data\" either does not exist, or it is not a folder.
The FileSystem:: also gives path not found
Copy-Item "FileSystem::\\192.168.0.000\Common Data\Software\Crystal Reports\CRRedist2005_X64.msi" -Destination $env:USERPROFILE\Downloads -verbose
If I copy the address into file explorer it works no problems.
I'm going to crazy trying to figure out why it's not working.
Edit: Remapped the drives and it worked on the second computer.
I'm currently using this Powershell script
Function Copy-ItemUNC {
New-PSDrive -Name "B" -PSProvider "FileSystem" -Root "\\ServerName\serverupdates\deploy\Program Files\"
Copy-Item -Path "\\servername\serverupdates\deploy\Program Files\*" -Destination 'C:/Program Files';
}
When I run the script it creates the folders but there are no subfolders within them.
Second problem I have is that I have to manually open Windows Explorer and type in the path to connect to it first in order for this script to even run. Is there a way to fix that too?
It's because you are not asking for subdirs, as that requires you to do this.
Copy-Item -Recurse
-Recurse <SwitchParameter>
Indicates that this cmdlet performs a recursive copy.
Required? false
Position? named
Default value False
Accept pipeline input? False
Accept wildcard characters? false
(Get-Command -Name Copy-Item).Parameters.Keys
Get-Help -Name Copy-Item -Full
Get-Help -Name Copy-Item -Examples
This command copies the mar1604.log.txt file to the C:\Presentation directory. The command does not delete the original file.
Example 2: Copy the contents of a directory to another directory
PS C:\>Copy-Item "C:\Logfiles" -Destination "C:\Drawings" -Recurse
So, this line should be like this..
Copy-Item -Path "\\servername\serverupdates\deploy\Program Files\*" -Destination 'C:/Program Files' -Recurse
You also do not really need this..
New-PSDrive -Name "B" -PSProvider "FileSystem" -Root "\\ServerName\serverupdates\deploy\Program Files\"
... based on what you are after. Especially since you are not using it anywhere in your code. That semi-colon is also not needed.
It's easy to copy multiple files to a folder that doesn't exists and let it be created:
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?
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.
If you're looking for a one liner solution, you can do this.
copy "C:\test2.txt" -Destination (New-Item "C:\Temp2\DoesNotExist\" -Type container -force) -Container -force
I think Geoff Guynn's one-liner should be as follows:
Copy-Item -Path "C:\test2.txt" -Destination (New-Item "C:\Temp2\DoesNotExist\" -ItemType directory -Force) -Force
The parameter for the cmdlet New-Item should be -ItemType and the intended "Type" should be directory.
The additional parameter -Container for the cmdlet Copy-Item seems to me superfluous; on the one hand it is set to $true by default anyway, on the other hand a single file should be copied and not the folder structure should be preserved.
I'm trying to copy a file to a new location, maintaining directory structure.
$source = "c:\some\path\to\a\file.txt"
destination = "c:\a\more\different\path\to\the\file.txt"
Copy-Item $source $destination -Force -Recurse
But I get a DirectoryNotFoundException:
Copy-Item : Could not find a part of the path 'c:\a\more\different\path\to\the\file.txt'
The -recurse option only creates a destination folder structure if the source is a directory. When the source is a file, Copy-Item expects the destination to be a file or directory that already exists. Here are a couple ways you can work around that.
Option 1: Copy directories instead of files
$source = "c:\some\path\to\a\dir"; $destination = "c:\a\different\dir"
# No -force is required here, -recurse alone will do
Copy-Item $source $destination -Recurse
Option 2: 'Touch' the file first and then overwrite it
$source = "c:\some\path\to\a\file.txt"; $destination = "c:\a\different\file.txt"
# Create the folder structure and empty destination file, similar to
# the Unix 'touch' command
New-Item -ItemType File -Path $destination -Force
Copy-Item $source $destination -Force
Alternatively, with PS3.0 onwards, you can simply use the New-Item to create the target folder directly, without having to create a "dummy" file, e.g. ...
New-Item -Type dir \\target\1\2\3\4\5
...will happily create the \\target\1\2\3\4\5 structure irrespective of how much of it already exists.
Here's a oneliner to do this. Split-Path retrieves the parent folder, New-Item creates it and then Copy-Item copies the file. Please note that the destination file will have the same filename as the source file. Also, this won't work if you need to copy multiple files to the same folder as with the second file you'll get An item with the specified name <destination direcory name> already exists error.
Copy-Item $source -Destination (New-Item -Path (Split-Path -Path $destination) -Type Directory)
I had files in a single folder in Windows 7 that I wanted to rename and copy to nonexistent folders.
I used the following PowerShell script, which defines a Copy-New-Item function as a wrapper for the Test-Item, New-Item, and Copy-Item cmdlets:
function Copy-New-Item {
$SourceFilePath = $args[0]
$DestinationFilePath = $args[1]
If (-not (Test-Path $DestinationFilePath)) {
New-Item -ItemType File -Path $DestinationFilePath -Force
}
Copy-Item -Path $SourceFilePath -Destination $DestinationFilePath
}
Copy-New-Item schema_mml3_mathml3_rnc schema\mml3\mathml3.rnc
# More of the same...
Copy-New-Item schema_svg11_svg_animation_rnc schema\svg11\svg-animation.rnc
# More of the same...
Copy-New-Item schema_html5_assertions_sch schema\html5\assertions.sch
# More of the same...
(Note that, in this case, the source file names have no file extension.)
If the destination file path does not exist, the function creates an empty file in that path, forcing the creation of any nonexistent directories in the file path. (If Copy-Item can do all that by itself, I could not see how to do it from the documentation.)
It is coming late, but as I stumbled upon this question looking for a solution to a similar problem, the cleanest one I found elsewhere is using robocopy instead of Copy-Item. I needed to copy the whole file structure together with the files, that's easily achieved via
robocopy "sourcefolder" "destinationfolder" "file.txt" /s
Detail about robocopy: https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/robocopy
None of the current answers worked for me to fix the Could not find a part of the path error raised by Copy-Item. After some research and testing, I discovered this error can be raised if the Destination path goes over the 260 character Windows path length limit.
What I mean by that is: if you supply a path to the Destination argument of Copy-Item and any of the files you are copying would exceed the 260 character limit when copied to the Destination folder, Copy-Item will raise the Could not find a part of the path error.
The fix is to shorten your Destination path, or to shorten/flatten the folder structure in the source directory that you are trying to copy.
May be Helpfull:
$source = 'c:\some\path\to\a\file.txt'
$dest = 'c:\a\more\different\path\to\the\file.txt'
$dest_dir = 'c:\a\more\different\path\to\the\'
[System.IO.Directory]::CreateDirectory($dest_dir);
if(-not [System.IO.File]::Exists($dest))
{
[System.IO.File]::Copy($source,$dest);
}
I have been digging around and found a lot of solutions to this issue, all being some alteration not just a straight copy-item command. Grant it some of these questions predate PS 3.0 so the answers are not wrong but using powershell 3.0 I was finally able to accomplish this using the -Container switch for copy-item.
Copy-Item $from $to -Recurse -Container
this was the test i ran, no errors and destination folder represented the same folder structure.
New-Item -ItemType dir -Name test_copy
New-Item -ItemType dir -Name test_copy\folder1
New-Item -ItemType file -Name test_copy\folder1\test.txt
#NOTE: with no \ at the end of the destination the file is created in the root of the destination, does not create the folder1 container
#Copy-Item D:\tmp\test_copy\* D:\tmp\test_copy2 -Recurse -Container
#if the destination does not exists this created the matching folder structure and file with no errors
Copy-Item D:\tmp\test_copy\* D:\tmp\test_copy2\ -Recurse -Container
I have two machines Server A and Server B, and I want to copy all the files and folder tree from Server A to Server B using PowerShell.
I have tried the command given below, but it copies only the files in the folder and does not create the folder tree:
Copy-Item E:\TestSource\* //TestDestination/ -recurse -force
If you have a source and a destination folder, you could use the following command:
robocopy $source $dest /e
If you need any more information about the command, you could use robocopy /?, or visit Robocopy documentation at Windows Server Technet, Robocopy.
Get-ChildItem \\server_a\c$ -Recurse | % {
$dest = Join-Path -Path \\server_b\c$ -ChildPath (Split-Path $_.FullName -NoQualifier)
if (!(Test-Path $dest))
{
mkdir $dest
}
Copy-Item $_.FullName -Destination $dest -Force
}
Using Split-Path with the -NoQualifier parameter will return the source path without the drive information. Join that with your destination path prefix and use the result to create the destination directory (if it does not exist) and perform the copy.
I would assume you have rights for the 2 servers
Get-ChildItem "\SERVER_A\C$" -Recurse | Copy-Item -Destination" \SERVER_B\C$"
Don't use PowerShell's Copy-Item cmdlet. Find a third-party mirroring/synchronization tool. We use RoboCopy.