I am using Copy-Item command to copy the multiple files from one location to another. In that, I want to get the count of files that are copied to destination.
If the above one is not possible, can we get 'True' if at least one file is copied and 'False' if none of the files are copied.
I tried this but could not able to get exact one.
Thanks,
Dinesh
use -passthru to create output for commands that normally run silent.
(Copy-Item -Recurse $srcPath -Destination $destPath -passThru).count
If you use -Verbose and redirect/capture the verbose stream, then you can compute the files copied from the verbose messages. This would look like
# run copy with -Verbose and capture stream 4 which is the verbose stream
$vml = Copy-Item -Recurse $srcPath -Destination $destPath -Verbose 4>&1
# Find all the messages with 'Copy File' in them
($vml -match "Copy File").Count
You probably also want to capture any errors using -ErrorVariable copyErrors.
Related
I used the Copy-Item command to copy a set reports to a server location. It was working fine before but recently more reports have been added to the folder and now most of the times it works fine but in some cases it shows the error:
The target file "$destination" is a directory, not a file
The code I used is:
Copy-Item -Path "$Source" -Destination "$destination" -Recurse -Force
I am not sure why I am not getting this error for every case.
What you have should work but this may work around the issue if there is some limitation with certain shared folder destinations:
Note: Remove the -WhatIf once you confirm the changes it will make are correct. Currently it will only output what files would have been copied.
Get-ChildItem $Source | Foreach-Object {
Copy-Item $_.FullName $destination -Recurse -Force -WhatIf
}
Basically, this enumerates all of the files and folders under the $Source directory, then copies the files or folders individually to the destination.
If you are able to offer the values of $Source and $Destination I or another might be able to offer a solution to your problem, rather than a workaround.
I know this has been asked a million times, but I can't seem to find anything that works for me. I don't know if there is a permissions issue or what, but I am trying to move files from one server to another using a PowerShell script in the task scheduler and it worked for about a week before it stopped working. There are no errors in the task scheduler, and I'm not well versed in PowerShell at all, I'm just trying to get something quick and simple for our CMS manager to move her files from the website to a folder on another server.
$ORG = "E:\folders\uploads\" ## enter current source folder
$DEST= "\\server-folder-structure\uploads\" ## enter your destination folder
foreach ($ORG in gci $DEST -include *.doc,*.docx,*.pdf,*.png,*.gif,*.jpg,*.jpeg,*.html,*.htm -recurse)
{
Move-Item -path $ORG -destination $DEST ## Move the files to the destination folder
}
I tried this too, in hopes it would work, but still no files are being moved.
Get-ChildItem E:\folder-structure\uploads\* -Include *.doc,*.docx,*.pdf,*.png,*.gif,*.jpg,*.jpeg,*.html,*.htm -Recurse |ForEach-Object { Move-Item $_.FullName \\server-folder-structure\uploads\ }
Am I doing something wrong? Are there permissions to folders that I need to set that I don't know about? Is PowerShell just not the best way to do this? Thanks in advance.
I believe you're making this harder than it should be (with respect to PowerShell being new to you). You don't need a loop on any of your examples if you want to pipe directly to Move-Item:
$ORG = "E:\folders\uploads\" ## enter current source folder
$DEST = "\\server-folder-structure\uploads\" ## enter your destination folder
$filterFor = "*.doc","*.docx","*.pdf","*.png","*.gif","*.jpg","*.jpeg","*.html","*.htm"
Get-ChildItem $ORG -Include $filterFor -File -Recurse |
Move-Item -Destination $DEST -WhatIf
As for what you tried, and as Mathias pointed out, you would be searching your $DEST location in which the files wouldn't exist as they would only be in $ORG; given that that's the actual source folder.
This would also overwrite you $ORG variable with the current item in your iteration in: foreach ($ORG in gci ..){ ... }.
Meaning, your Move-Item would be invalid.
I have a strange issue with the Copy-Item in PowerShell. Below you see two almost identical lines of code, only the source and destination is different.
Copy-Item "A:\*" -Destination "B:\" -Recurse -Force -Verbose -Confirm:$false -ErrorAction SilentlyContinu
Start-Sleep 3
Copy-Item "B:\*" -Destination "D:\" -Recurse -Force -Verbose -Confirm:$false -ErrorAction SilentlyContinu
These lines are a part of a script. It first notes the date and creates a log file and then it clears the B and D drive. After that, it starts copying. The first one, the Copy-Item A:\ to B:\ works, it copies everything.
When PowerShell gets to the second one, it only copies a few folders and just skips over the remaining folders. It doesn't throw me an error or anything.
When I manually execute the second line, it works, but not when it's in a script.
What is wrong here?
I'd use robocopy for this, it's included in all modern versions of windows and is very powerful and can complete pretty much any copy function you want.
ROBOCOPY /MIR <Source> <Target>
/MIR is the mirror switch. It will both copy source to target and delete anything in target that isn't present in source. Leaving target as a mirror copy of source.
You might also want to look into the /MT multi-thread switch to speed things up if the directories contain a large number of files.
I have a .txt with the names of over 1000 files I want to delete. My .txt file does not have file paths. The files I want to delete are spread throughout multiple folders but they are all in the same drive. Is there any way to use powershell or command prompt to search for all files within my drive with the same name as what is listed in my .txt file and delete them?
Assuming you're PowerShell prompt is currently set at the root location from which you want to start your search and the file is in the same directory:
gc .\MyListOfFilesIWantToDelete.txt | %{gci $_ -Recurse | Remove-Item -WhatIf}
Note, you'll have to remove the -whatif
Or, let's say your file is somewhere else where you have PowerShell opened (eg: ~/Documents), and you want to scan your D: drive. This should work:
gc .\MyListOfFilesIWantToDelete.txt | %{gci D:\ -Filter $_ -Recurse -ErrorAction SilentlyContinue | Remove-Item -WhatIf}
Note I put SilentlyContinue. This is because you'll see a lot of red if you don't have access to folders in your search path.
Alternatively, you can load up a variable with your list of files..
$thesefiles = gc .\mylistoffilesiwanttodelete.txt
.. and use the Remove-Item cmdlet directly..
Remove-Item -Path D:\Folder -Include $thesefiles -Recurse -WhatIf
or in one swoop without loading a variable:
Remove-Item -Path D:\Folder -Include $(gc .\mylistoffilesiwanttodelete.txt) -Recurse -WhatIf
Again, I'm using -WhatIf for testing. Also, I've noticed different behaviors in the past with get-childitem on different versions of PowerShell. I tested these with 5.1
Change directory from following powershell command
Following command will allow you to delete .txt files in specific directory
Get-ChildItem C:\*.txt -file -r | remove-item
I'm very new to this but I have a problem with deleting 30days old files which I found an answer to here: Powershell - Delete subfolder(s) in folder with specific name(s) older than 30 days
But I would like to make a follow question on that.
The code I use is this:
gci P:\ -directory -recurse | ?{$_.FullName -match ".:\\.+?\\.+?\\.+?\\.+?\\.+?\\" -and $_.CreationTime -lt (get-date).AddDays(-30)}|Remove-Item -recurse -whatif
Is it possible to log what is deleted as well? Would be awesome if the size of the files are included in the log file. Thanks!
Make the remove operation verbose and redirect the verbose stream to a file:
... | Remove-Item -Recurse -Verbose 4> 'C:\path\to\your.log'
Note that this requires at least PowerShell v3.
If you only want to log what would be deleted without actually deleting it, use -WhatIf instead of -Verbose:
... | Remove-Item -Recurse -WhatIf
You can also combine the two:
$dryrun = $true # set to $false to actually delete
... | Remove-Item -Recurse -Verbose -WhatIf:$dryrun 4> 'C:\path\to\your.log'
However, -WhatIf output goes to the host console, so it can't be redirected to a file. You could use Start-Transcript as a workaround, but that would record everything, not just the would-be deletes. Or you could run the entire code/script (without redirections) in a new PowerShell process:
powershell.exe -File 'C:\path\to\your.ps1' > 'C:\path\to\your.log'
The host output of a child PowerShell process is merged into its STDOUT, so you can redirect it "from the outside".