Combine the results of two distinct Get-ChildItem calls into single variable to do the same processing on them Combine the results of two distinct Get-ChildItem calls into single variable to do the same processing on them powershell powershell

Combine the results of two distinct Get-ChildItem calls into single variable to do the same processing on them


Not sure you need a generic list here. You can just use a PowerShell array e.g.:

$items  = @(Get-ChildItem '\\server\C$\Program Files (x86)\Data1\' -r)$items += @(Get-ChildItem '\\server\C$\Web\DataStorage\' -r)

PowerShell arrays can be concatenated using +=.


From get-help get-childitem: -Path Specifies a path to one or more locations. Wildcards are permitted. The default location is the current directory (.).

$items = get-childitem '\\server\C$\Program Files (x86)\Data1\','\\server\C$\Web\DataStorage\' -Recurse


Here is some perhaps even more PowerShell-ish way that does not need part concatenation or explicit adding items to the result at all:

# Collect the results by two or more calls of Get-ChildItem# and perhaps do some other job (but avoid unwanted output!)$result = .{    # Output items    Get-ChildItem C:\TEMP\_100715_103408 -Recurse    # Some other job    $x = 1 + 1    # Output some more items    Get-ChildItem C:\TEMP\_100715_110341 -Recurse    #...}# Process the result items$result

But the code inside the script block should be written slightly more carefully to avoid unwanted output mixed together with file system items.

EDIT: Alternatively, and perhaps more effectively, instead of .{ ... } we canuse @( ... ) or $( ... ) where ... stands for the code containing severalcalls of Get-ChildItem.