powershell

PowerShell: How plug in filter (condition for exists) in Get-Childitem


is it possible to add condition into DIR code so it will perform last pipe operation only for files which exist in the names list? something like if ($names -contains $_.BaseName) {... Sorry it's hard to provide ready to use code, so any ideas or pseudo code will be appreciated. I don't want to deal with change logic to the loop.

$excelData = Import-Excel -Path "C:\TSQ\ControlCard.xlsx" -WorksheetName "Sheet1"
$names = $excelData.Name
 ###---- write RS only if $_.BaseName exists in names list    
DIR $currSourceFolder -Filter *.dat | % { $_.FullName } | Write-RsCatalogItem -Proxy $Proxy -Destination $currTargetFolder 

Solution

  • The path can contain an array of names, but you'd either have to cd to the folder first, or prefix the folder to each name. Unfortunately, the filter can't be an array. The path can have wildcards too.

    cd $currSourceFolder
    dir $names
    
    dir ($names | % { "$currentSourceFolder\$_" })