0
votes

I'm new to PowerShell. Spent several days trying to get this, think I am very close, but need an expert to show me the final step.

Using PowerShell v5, Need to search for folder(s) which match specific name, then copy those folders and their files, AND the path to the Folders to another drive.

Script now:

Get-ChildItem s:\ -Filter (Read-Host -Prompt "Enter") -Recurse -ErrorAction SilentlyContinue -Force | 
    Select-Object FullName | ForEach-Object {Copy-Item -Path $_.FullName -Destination 'C:\Robotics\AA\ConfigFiles\Automation Anywhere Files\Automation Anywhere\My Tasks\' -recurse -Force}

This does a search for the Folder, returns results and then copies the folder and contents into the destination location.

The problem is, I actually needed the source path appended to my destination path. Source is variable number of folders deep.

Any suggestions?

1
So the destination path is the found path in which S:\ should be replaced with C:\Robotics\AA\ConfigFiles\Automation Anywhere Files\Automation Anywhere\My Tasks\ ? Is there a reason to write this as a one liner?user6811411

1 Answers

0
votes
  • To limit the Get-ChildItem to folders I inserted -Directory
  • Copy-Item accepts input from the pipeline, no ForEach-Object necessary.
  • the -replace is RegEx based and requires the backslash in the path S:\ to be escaped by another backslash S:\\.

$DestBase = 'C:\Robotics\AA\ConfigFiles\Automation Anywhere Files\Automation Anywhere\My Tasks\'
$Search = Read-Host -Prompt "Enter folder:"
Get-ChildItem -Path S:\ -Filter $Search -Directory -Recurse -ErrorAction SilentlyContinue -Force | 
    Copy-Item -Destination {$($_.Fullname) -replace 'S:\\',"$DestBase"} -Recurse -Force -Whatif

If the output looks OK, remove the -WhatIf paramter in the last line.