none
Different behavior in -exclude parameter for get-childitem cmdlet when using a variable to hold the items to be excluded or specifying them as a literal RRS feed

  • Question

  • I'm trying to exclude several items from a get-childitem cmdlet call using the -exclude parameter. The script produces what I want IF I specify the exclude items as a literal in the command but if I specify the items in a variable the items are not excluded. I've tried splatting the variable but that didn't work either. Here is the code;

    Param(
       [Parameter(Mandatory=$True,Position=1)]
       [string]$attempt
    )
    
    $path = "c:\dev\ccm\wa\lgc\1011\lgc"
    $exclude = "rtcs,.vscode,*.inf"
    $ofile = "c:\temp\lgc-file-list$attempt.txt"
    $cmd = "get-childitem -path $path -exclude rtcs,.vscode,*.inf | get-childitem -recurse -file >> $oFile"
    add-content -Path $oFile -Value $cmd
    #
    # This works as expected with the items being excluded from the output.
    #
    get-childitem -path $path -exclude rtcs,.vscode,*.inf | get-childitem -recurse -file >> $ofile
    
    #
    # This doesn't work. I get output but the items are not excluded.
    #
    get-childitem -path $path -exclude $exclude | get-childitem -recurse -file >> $ofile

    Any idea why I'm getting different behavior?

    Thursday, May 7, 2020 10:17 PM

All replies

  • $exclude = @('rtcs', '.vscode', '*.inf')


    \_(ツ)_/

    • Marked as answer by TxDot Friday, May 8, 2020 1:31 PM
    • Unmarked as answer by TxDot Tuesday, May 19, 2020 6:41 PM
    Thursday, May 7, 2020 11:06 PM
  • $exclude = @('rtcs', '.vscode', '*.inf')


    \_(ツ)_/

    As pointed out by jrv, $exclude is an array. You could also use foreach()

    Friday, May 8, 2020 3:52 AM
  • $exclude = @('rtcs', '.vscode', '*.inf')


    \_(ツ)_/

    Out of all the variations I tried, that was not one of them and turned out to be the one that works. Thanks jrv!
    Friday, May 8, 2020 1:33 PM
  • $exclude = @('rtcs', '.vscode', '*.inf')


    \_(ツ)_/

    As pointed out by jrv, $exclude is an array. You could also use foreach()

    True enough but that is so much more wordy than adding a @ and () to the declaration of the variable. 
    Friday, May 8, 2020 1:35 PM
  • $exclude = @('rtcs', '.vscode', '*.inf')


    \_(ツ)_/

    Well, it turns out that this answer didn't work. It was an error on my part indicating that it did work. The reason for my error is that the output was placed in a different location in the output and I was not expecting that so I didn't check for it.

    I think at this point we're looking at an error with the -exclude parameter of the get-childitem cmdlet.

    Tuesday, May 19, 2020 6:41 PM
  • There is no error in the exclude parameter.  The error is with the usage or with the visibility of the objects.  They must be visible to your session as logged in.

    What are you trying to exclude?  Can you see the objects when you don't use the exclude parameter?


    \_(ツ)_/

    Tuesday, May 19, 2020 7:52 PM