Search code examples
arrayspowershellcopy-item

How to search Powershell array for specific strings and use this result in a copy-item -include script


I'm trying to create a synchronization script in Powershell so that my applications in MDT are being copied on a regular basis to our main file server, based on the folder name (in MDT, applications are in one folder, where our main server has applications split depending on the department who uses them).

From what I read on the web, the best way would be to populate an array with "Get-ChildItem", which I kinda figured how to do (see code below).

After the array is populated though, I don't know how to search that array for specific results, nor do I know how to use those results with copy-item.

In a nutshell, here's what I need to do: Build an array using "Get-ChildItem", query the resulting array for specific folders, and have those folders be copied to specific destinations.

Here's the code I have so far:

$arr = Get-ChildItem \\slmtl-wds02.domain.inc\deploymentshare$\applications | 
   Where-Object {$_.PSIsContainer} | 
   Foreach-Object {$_.Name}
$sourcepath = \\slmtl-wds02.domain.inc\deploymentshare$\applications
$destSLARC = \\slmtl-fs01.domain.inc\folder\it_services\private\software\service_desk\pc\SLARCMTL
$destSLMTL = \\slmtl-fs01.domain.inc\folder\it_services\private\software\service_desk\pc\SLMTL
$destSLGLB = \\slmtl-fs01.domain.inc\folder\it_services\private\software\service_desk\pc\SLGLB
$destSLTECH = \\slmtl-fs01.domain.inc\folder\it_services\private\software\service_desk\pc\SLTECH

Thanks in advance for your help :)


Solution

  • $sourceLocation = "c:\analysis\"
    $targetLocation = "c:\analysisCopy\"
    $included = @("folder1", "folder2")
    
    $result = @()
    foreach ($i in $included){
        $result += get-ChildItem $sourceLocation -filter $i | Where-Object {$_.PSIsContainer}
    }
    $result | foreach-Object { copy-item $_.FullName -Destination $targetLocation -Recurse}