I am new to Powershell, and am testing out the functionality of Copy-Item according to this documentation. I am trying to copy files and folders from a subfolder into it's containing folder, but it is not working. What command do I need to use?
Here is my sample file structure
C:\
Example
|
|__Top
|
|__Middle
|
|__Bottom
I tried to copy all the files and subfolders from Middle into Top using
Copy-Item "C:\Example\Top\Middle" -Destination "C:\Example\Top" -Recurse
but received this error
Copy-Item : An item with the specified name C:\Example\Top\Middle already exists.
At line:1 char:1
+ Copy-Item "C:\Example\Top\Middle" -Destination "C:\Example\Top" -Recu ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ResourceExists: (C:\Example\Top\Middle:String)
[Copy-Item], IO
Exception
+ FullyQualifiedErrorId :
DirectoryExist,Microsoft.PowerShell.Commands.CopyItemCommand
Copy-Item : Cannot overwrite the item C:\Example\Top\Middle\InTheMiddle.txt
with itself.
At line:1 char:1
+ Copy-Item "C:\Example\Top\Middle" -Destination "C:\Example\Top" -Recu ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : WriteError:
(C:\Example\Top\Middle\InTheMiddle.txt:String) [Co
py-Item], IOException
+ FullyQualifiedErrorId :
CopyError,Microsoft.PowerShell.Commands.CopyItemCommand
Copy-Item : An item with the specified name C:\Example\Top\Middle\Bottom already
exists.
At line:1 char:1
+ Copy-Item "C:\Example\Top\Middle" -Destination "C:\Example\Top" -Recu ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ResourceExists:
(C:\Example\Top\Middle\Bottom:String) [Copy-It
em], IOException
+ FullyQualifiedErrorId :
DirectoryExist,Microsoft.PowerShell.Commands.CopyItemCommand
Copy-Item : Cannot overwrite the item
C:\Example\Top\Middle\Bottom\InTheBottom.txt with
itself.
At line:1 char:1
+ Copy-Item "C:\Example\Top\Middle" -Destination "C:\Example\Top" -Recu ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : WriteError:
(C:\Example\Top\...InTheBottom.txt:String) [Copy-I
tem], IOException
+ FullyQualifiedErrorId :
CopyError,Microsoft.PowerShell.Commands.CopyItemCommand
Powershell Version 5.1.17134.112 Running as Administrator
You trying to copy child folder to its parent folder. To copy content of "Middle" folder you need to use this command:
Copy-Item "C:\Example\Top\Middle\*" -Destination "C:\Example\Top" -Recurse
I believe this will accomplish what you are looking for as the simplest example. It would need tweaked depending on your scenario.
-Force would need applied to overwrite.
$source = "C:\Example\Top\Middle\*"
$destination = "C:\Example\Top\"
Copy-Item -Path $source -Destination $destination -Include *.*
Related
In GNU/Linux, I can just do this:
tar cfz hierarchy.tgz hierarchy
and it just works, even if some of the items in hierarchy are being used by other processes.
In Windows, I can right-click on a folder and send it to a compressed zip file, so long as it's in another place, like the Desktop, and that works.
But in Windows PowerShell, something you would think would be so simple, isn't:
PS E:\> Compress-Archive -Path e:\lib -DestinationPath .\e-lib-all.zip
ZipArchiveHelper : The process cannot access the file
'E:\lib\company\data\data-azure-java\1.0.1\data-azure-java-1.0.1.jar' because it is being used by another process.
At
C:\Windows\system32\WindowsPowerShell\v1.0\Modules\Microsoft.PowerShell.Archive\Microsoft.PowerShell.Archive.psm1:697
char:30
+ ... sArchived = ZipArchiveHelper $subDirFiles.ToArray() $destinationPath ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : PermissionDenied: (E:\lib\company...-java-1.0.1.jar:String) [Write-Error], IOException
+ FullyQualifiedErrorId : CompressArchiveUnauthorizedAccessError,ZipArchiveHelper
New-Object : Exception calling ".ctor" with "1" argument(s): "Stream was not readable."
At
C:\Windows\system32\WindowsPowerShell\v1.0\Modules\Microsoft.PowerShell.Archive\Microsoft.PowerShell.Archive.psm1:808
char:38
+ ... $srcStream = New-Object System.IO.BinaryReader $currentFileStream
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [New-Object], MethodInvocationException
+ FullyQualifiedErrorId : ConstructorInvokedThrowException,Microsoft.PowerShell.Commands.NewObjectCommand
I asked a coworker, what's the PowerShell equivalent of the GUI way that just works? He thought what the GUI does in the background is create a copy using service level rather than user permissions, and then zips that.
Is there a PowerShell way to do this? I've tried some other things that did not work, namely:
Copy-Item -Path "E:\lib" -Force -PassThru | `
Get-ChildItem | `
Compress-Archive -DestinationPath "E:\e-lib-all.zip"
Get-ChildItem -Recurse -Path $sourcePath | Compress-Archive -DestinationPath $destinationPath
Get-ChildItem -Recurse -Path "E:\lib" | Compress-Archive -DestinationPath "E:\e-lib-all.zip"
Yes, I looked at previous SO post Compress-Archive error: PermissionDenied but it didn't help (I got the same errors).
I want copy/paste all files and all folder in other path.
May data is:
temp
Deploiement
foo.xml
bar.xml
Documentation
brute
I read official documentation on https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.management/copy-item?view=powershell-7.1 and I find this example:
Example 3: Copy directory and contents to a new directory
Copy-Item -Path "C:\Logfiles\*" -Destination "C:\Drawings\Logs" -Recurse
In my Jenkins, I write this:
Copy-Item -Path "temp/Deploiement/*" -Destination "Deploy" -Recurse
Console error:
Copy-Item : Container cannot be copied onto existing leaf item.
At D:\JenkinsWks\workspace\pic\temp\Scripts\packer.ps1:114 char:2
+ Copy-Item -Path "temp/Deploiement/*" -Destination "Deploy" - ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (D:\JenkinsWks\w...t\Documentation:String) [Copy-Item], PSArgumentException
+ FullyQualifiedErrorId : CopyContainerItemToLeafError,Microsoft.PowerShell.Commands.CopyItemCommand
Copy-Item : Container cannot be copied onto existing leaf item.
At D:\JenkinsWks\workspace\pic\temp\Scripts\packer.ps1:114 char:2
+ Copy-Item -Path "temp/Deploiement/*" -Destination "Deploy" - ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (D:\JenkinsWks\w...nt\brute:String) [Copy-Item], PSArgumentException
+ FullyQualifiedErrorId : CopyContainerItemToLeafError,Microsoft.PowerShell.Commands.CopyItemCommand
if (-Not (Test-Path "Deploy")) {
md -path "Deploy"
}
Copy-Item -Path "temp/Deploiement/*" -Destination "Deploy" -Recurse
I'm a Powershell novice. I've written a .ps1 script to do a number of things, all of which execute perfectly, except when it comes to a "Copy-Item" command, which I'm using to copy contents of the (Windows 10) desktop to another folder.
It's failing at the hyphen in "Copy-Item". I have a feeling I'm either missing a "*" in one of the paths. or have one where it should not be. I've tried it with various combinations, but no joy.
For test purposes, I have 3 items on the desktop: 1) a folder shortcut, 2) a Powershell script shortcut, and 3) A GodMode 'folder'.
Thank you in advance for your help. Following is a subset of the code:
$env:path += ";D:\PowershellScriptsFolder" # this is the location of my ps1
# (a bunch of code here, all of which runs fine) #
# Desktop
# Define Variables
$DestinationPath = "D:\folder1\subfolder\*"
$SourcePathRoot = 'C:\Users\Sfrn\Desktop\' # this is the location of my Win 10 desktop
#
Remove-Item -Recurse -Path $DestinationPath -Force
(*** Here's where it fails: ***)
Copy-Item -Recurse -Path $SourcePathRoot -Destination $DestinationPath -Force # this is line 32
(*** Here's the error output - character 5 is the hyphen in "Copy-Item": ***)
Copy-Item : Illegal characters in path.
At D:\Dropbox\DB_AppData\PowerShell_and_Bat\CopyToBackup_2.ps1:32 char:5
Copy-Item -Recurse -Path $SourcePathRoot -Destination $Destinatio ...
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
CategoryInfo : InvalidArgument: (D:\Dropbox\DB_AppData\Desktop*:String) [Copy-Item], ArgumentException
FullyQualifiedErrorId : CreateDirectoryArgumentError,Microsoft.PowerShell.Commands.CopyItemCommand
Copy-Item : Illegal characters in path.
At D:\Dropbox\DB_AppData\PowerShell_and_Bat\CopyToBackup_2.ps1:32 char:5
Copy-Item -Recurse -Path $SourcePathRoot -Destination $Destinatio ...
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
CategoryInfo : InvalidArgument: (desktop.ini:FileInfo) [Copy-Item], ArgumentException
FullyQualifiedErrorId : CopyDirectoryInfoItemArgumentError,Microsoft.PowerShell.Commands.CopyItemCommand
Copy-Item : Illegal characters in path.
At D:\Dropbox\DB_AppData\PowerShell_and_Bat\CopyToBackup_2.ps1:32 char:5
Copy-Item -Recurse -Path $SourcePathRoot -Destination $Destinatio ...
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
CategoryInfo : InvalidArgument: (Dropbox.lnk:FileInfo) [Copy-Item], ArgumentException
FullyQualifiedErrorId : CopyDirectoryInfoItemArgumentError,Microsoft.PowerShell.Commands.CopyItemCommand
Copy-Item : Illegal characters in path.
At D:\Dropbox\DB_AppData\PowerShell_and_Bat\CopyToBackup_2.ps1:32 char:5
Copy-Item -Recurse -Path $SourcePathRoot -Destination $Destinatio ...
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
CategoryInfo : InvalidArgument: (OpenApps.lnk:FileInfo) [Copy-Item], ArgumentException
FullyQualifiedErrorId : CopyDirectoryInfoItemArgumentError,Microsoft.PowerShell.Commands.CopyItemCommand
Copy-Item : Illegal characters in path.
At D:\Dropbox\DB_AppData\PowerShell_and_Bat\CopyToBackup_2.ps1:32 char:5
Copy-Item -Recurse -Path $SourcePathRoot -Destination $Destinatio ...
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
CategoryInfo : InvalidArgument: (D:\Dropbox\DB_AppData\Desktop*:String) [Copy-Item], ArgumentException
FullyQualifiedErrorId : CreateDirectoryArgumentError,Microsoft.PowerShell.Commands.CopyItemCommand
(*** End of error output: ***)
(a bunch of code continues to run here, all of which is fine)
END
You have an asterisk in the wrong spot. It needs to be in the source directory path, not the destination, like this:
$SourcePathRoot = "C:\Users\Sfrn\Desktop\*"
$DestinationPath = "D:\folder1\subfolder\"
i have to delete folders based on a excel list, so i have tried to import the execl with import csv but it dosent work.
The import doesnt fill the variable in the correct format.
This is the code i tried to use:
$folders = import-csv G:\Book1.csv foreach ($folder in $folders) {Remove-Item -Path $folder -Recurse -Force}
The error is this:
Remove-Item : Cannot find drive. A drive with the name '#{Foldername=G' does not exist.
At line:4 char:5
+ Remove-Item -Path $folder -Recurse -Force
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (#{Foldername=G:String) [Remove-Item], DriveNotFoundException
+ FullyQualifiedErrorId : DriveNotFound,Microsoft.PowerShell.Commands.RemoveItemCommand
Remove-Item : Cannot find drive. A drive with the name '#{Foldername=G' does not exist.
At line:4 char:5
+ Remove-Item -Path $folder -Recurse -Force
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (#{Foldername=G:String) [Remove-Item], DriveNotFoundException
+ FullyQualifiedErrorId : DriveNotFound,Microsoft.PowerShell.Commands.RemoveItemCommand
Remove-Item : Cannot find drive. A drive with the name '#{Foldername=G' does not exist.
At line:4 char:5
+ Remove-Item -Path $folder -Recurse -Force
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (#{Foldername=G:String) [Remove-Item], DriveNotFoundException
+ FullyQualifiedErrorId : DriveNotFound,Microsoft.PowerShell.Commands.RemoveItemCommand
In the CSV in the 1.Line heading stands foldername in line 2-4 are the name of the folders (a, b, c).
This is the CSV:
Foldername
G:\Test\a
G:\Test\b
G:\Test\c
The directory is G:\Test\a , b, c
I have found a Solution, the CSV file was a Problem and the import so i had to modify the CSV File:
"Foldername"
"G:\Test\a"
"G:\Test\b"
"G:\Test\c"
the powershell code is this now:
$folders = import-csv .csv | ForEach-Object {
write-host $($_.Foldername)
Remove-Item -Path $($_.Foldername) -Recurse -Force
}
I have two scripts which should do the same but the first doesn't work and the second does. Why is that? I have managed to figure out it has something to do with the Execlude. It says:
Move-Item : Cannot move item because the item at 'E:\hudba\test.ps1' does not exist. At line:1 char:1 + Move-Item "E:\test\*" "E:\ ... + CategoryInfo : InvalidOperation: (:) [Move-Item], PSInvalidOperationException + FullyQualifiedErrorId : InvalidOperation,Microsoft.PowerShell.Commands.MoveItemCommand
The scripts
Move-Item "E:\test\*" "E:\test2" -Exclude "*.ps1"
Get-ChildItem -Path "E:\test\*" -Recurse -Exclude "*.ps1" | Move-Item -Destination "E:\test2"
Thank you :)
This is a known issue, and is slated for v6 to fix
https://github.com/PowerShell/PowerShell/issues/2385