Get-ChildItem and Copy-Item explanation - powershell

Why does
gci $from -Recurse | copy-item -Destination $to -Recurse -Force -Container
not behave in the same way as
copy-item $from $to -Recurse -Force
?
I think it should be the same, but somehow it's not. Why?

You are not looping over each item in the collection of files/folders, but passing the last value to the pipe. You need to use Foreach-item or % to the Copy-Item command. From there, you also do not need the second -recurse switch as you already have every item in the GCI.
try this:
gci $from -Recurse | % {copy-item -Path $_ -Destination $to -Force -Container }

Here is what worked for me
Get-ChildItem -Path .\ -Recurse -Include *.txt | %{Join-Path -Path $_.Directory -ChildPath $_.Name } | Copy-Item -Destination $to

This works for me:
Get-ChildItem 'c:\source\' -Recurse | % {Copy-Item -Path $_.FullName -Destination 'd:\Dest\' -Force -Container }

To loop all items, this should work:
gci -Path $from -Recurse | % { $_ | copy-item -Destination $to -Force -Container}
Just do the foreach and pipe each item again.

The switches are wrong. They should be:
gci -Path $from -Recurse | copy-item -Destination $to -Force -Container

Related

Moving files in powershell into different location

It's supposed to move all files in $source into $destination. But something is not working here..
$source = Get-ChildItem 'E:\files' -Recurse
$destination = Get-ChildItem "C:\destination" -Recurse
foreach($file in $source)
{
Move-Item -Path $source -Destination $destination
}
This is how you are supposed to do it:
$source = Get-ChildItem -Path X:\Directory -Recurse
$destination = "Y:\Directory"
foreach ($item in $source) { Move-Item -Path $item -Destination $destination }
A one-liner would be:
Get-ChildItem -Path X:\Directory -Recurse | Move-Item -Destination Y:\Destination
Your first mistake was the declaration of the destination variable: It should be just $destination = "C:\Destination"; and, second, you created an "index" named $file but you did not use it inside the foreach, which should be Move-Item -Path $file -Destination $destination.

Batch Replace-Name & Move-item

I got a script:
$CopySource = "C:\Source\Qlikview Storage\PrivateData\Gemensamma\Qvd_Raw\Agresso"
$CopyDestination = "C:\Dest\Qlikview Storage\PrivateData\Gemensamma\Qvd_Raw\Agresso"
$files = Get-ChildItem -File "*.qvd" $CopySource -Force
foreach ($file in $files) {
Copy-Item -path $CopySource\$_$file -Destination $CopyDestination -Force
}
$CopySource = "C:\Source\Qlikview Storage\PrivateData\Gemensamma\Qvd_Raw\Agresso"
$CopyDestination = "C:\Dest\Qlikview Storage\PrivateData\Gemensamma\Qvd_Raw\Agresso"
$items = Get-ChildItem -File "*.qvd" $CopyDestination
foreach($I in $Items) {
$newfilename= rename-item -path $I.Name -newname ("Agresso_" + $I.Name)
The problem I have is that I need to copy files from source to destination and once they are over they need to have agresso_ added. Then they day after a batch of files will be copied again and also they need to be renamed to agresso_ and overwrite the old ones, preferably with move-item. I got a problem already, as I am not used with prefixes,
I tried enless of versions with where-object and simular, could not figure out a way to use test-path either.
I did exactly this but with renaming the files, like this for maximo:
$files = Get-ChildItem -File "*.qvd" $CopySource -Force
foreach ($file in $files) {
Copy-Item -path $CopySource\$_$file -Destination $CopyDestination -Force -Verbose 4>&1 |
Out-File -Append $logpath
}
"Klar med Kopiering av .qvd filer $global:currenttime" | Out-File $logpath -Append
"Påbörjar omdöpning av .qvd filer $global:currenttime" | Out-File $logpath -Append
$items = Get-ChildItem -File "*.qvd" $CopyDestination
foreach($I in $Items) {
$newfilename=$I.Name.Replace("QVDLager1","Maximo")
Move-Item -Path $I.FullName -Destination $CopyDestination\$newfilename -Force -Verbose 4>&1 |
Out-File -Append $logpath
If anyone can help me in the right direction it would be highly appriciated.
You can copy (or move) and rename the destination at the same time:
$CopySource = "C:\Source\Qlikview Storage\PrivateData\Gemensamma\Qvd_Raw\Agresso"
$CopyDestination = "C:\Dest\Qlikview Storage\PrivateData\Gemensamma\Qvd_Raw\Agresso"
Get-ChildItem -Path $CopySource -File "*.qvd" -Force | ForEach-Object {
# create the full path for the target file with "Agresso_" prefixed
$target = Join-Path -Path $CopyDestination -ChildPath ('Agresso_{0}' -f $_.Name)
$_ | Copy-Item -Destination $target -WhatIf
}
If satisfied with the results of the code shown in the console, you can remove the -WhatIf switch to really start copying (or moving) the files.

Powershell find and move file

I have a long list of find and move operations, I want to slim down the script by substituting the following 2 long commands with 2 short variables.
Short variables:
$f = 'Get-ChildItem -Recurse -Filter'
$m = 'Move-Item -Force -Verbose -Destination V:\MSL\_pdf\'
Long commands:
Get-ChildItem -Recurse -Filter GAS*.pdf | Move-Item -Force -Verbose -Destination V:\MSL\_pdf\GAS
Get-ChildItem -Recurse -Filter GCA_00*.pdf | Move-Item -Force -Verbose -Destination V:\MSL\_pdf\GCA\GCA_00
Get-ChildItem -Recurse -Filter GCA_01*.pdf | Move-Item -Force -Verbose -Destination V:\MSL\_pdf\GCA\GCA_01
And this doesn't work:
$f GAS*.pdf | $m`GAS
$f GCA_00*.pdf | $m`GCA\GCA_00
$f GCA_01*.pdf | $m`GCA\GCA_01
As #Lee said in the comment, you should use function:
function GetAndMove($files, $dest)
{
$dest = "V:\MSL\_pdf\$($dest)"
Get-ChildItem -Recurse -Filter $files | Move-Item -Force -Verbose -Destination $dest
}
# Now call the function
GetAndMove "GAS*.pdf" "GAS"
GetAndMove "GCA_00*" "GCA\GCA_00"
GetAndMove "GCA_01*" "GCA\GCA_01"
...

Move-Item doesn't move files

I am trying to move a bunch of files in child directories into the parent directory. I have verified that the individual search works and that my concatenations are correct. However, the Move-Item does.. nothing.
I can't figure out why.
Get-ChildItem $pwd -name -recurse *.docx | foreach{ Move-Item (Join-Path . $_) .}
Any help would be appreciated.
Thanks.
Edit: Updated with the solution specific to my problem based on J.Drexler's explanation/answer.
Get-ChildItem $pwd -name -recurse *.docx | Move-Item -Destination .
It's done like this :
Get-ChildItem $path -Filter "*.docx" | move-item -Destination (split-path $path -Parent )
Note: You don't need a "foreach" after a "|".
If you use "|" after a listing-command, its the same like:
$items = GetList $path
foreach($item in $items){ DoSomething $item }
this is same as:
GetList $path | DoSomething

Copy Folder To A Range Of Computers On LAN

I want to copy a folder to a range of computers on my LAN. Here is what I have so far:
$Computers = "Get-Content C:\Scripts\computers.txt"
$Source = "C:\Install\9_10_00_08HotFix_201504140001"
$Destination = "\\192.168.6.$\c$\Install"
ForEach-Object {
Copy-Item -Path $Source -Recurse -Destination $Destination -Verbose -Force -ErrorAction SilentlyContinue
}
You didn't say what your actual problem is, but it's probably that you misplaced the first double quote in the Get-Content statement, that you can't define a variable destination like you do, and that you don't do anything with what you (try to) read from computers.txt.
Change this:
$Computers = "Get-Content C:\Scripts\computers.txt"
$Source = "C:\Install\9_10_00_08HotFix_201504140001"
$Destination = "\\192.168.6.$\c$\Install"
ForEach-Object {
Copy-Item -Path $Source -Recurse -Destination $Destination -Verbose -Force -ErrorAction SilentlyContinue
}
into this:
$Computers = Get-Content "C:\Scripts\computers.txt"
$Source = "C:\Install\9_10_00_08HotFix_201504140001"
$Computers | ForEach-Object {
Copy-Item -Path $Source -Recurse -Destination "\\192.168.6.$_\c$\Install" -Verbose -Force -ErrorAction SilentlyContinue
}
and your code should work.