Populate an array from text file

This topic contains 2 replies, has 2 voices, and was last updated by Profile photo of Bob McCoy Bob McCoy 2 years ago.

  • Author
    Posts
  • #20838
    Profile photo of Tony Wainwright
    Tony Wainwright
    Participant

    Hi
    I am wanting to get a list of extensions in a folder, but exclude a list of known extension that are stored in a text file(one per line). Using the following code:

    $Exclude = Get-Content C:\Scripts\ExcludeExt.txt
    Get-Childitem -Recurse |
     Where-Object { (-not $.PSIsContainer) -and ($Exclude -notcontains $.Extension) }

    but get a lot of errors
    [blockquote]At line:3 char:22
    + Where-Object { (-not $.PSIsContainer) -and ($Exclude -notcontains $.Extension) ...
    + ~
    Missing expression after unary operator '-not'.
    At line:3 char:23
    + Where-Object { (-not $.PSIsContainer) -and ($Exclude -notcontains $.Extension) ...
    + ~~~~~~~~~~~~~~~
    Unexpected token '$.PSIsContainer' in expression or statement.
    At line:3 char:22
    + Where-Object { (-not $.PSIsContainer) -and ($Exclude -notcontains $.Extension) ...
    + ~
    Missing closing ')' in expression.
    At line:3 char:15
    + Where-Object { (-not $.PSIsContainer) -and ($Exclude -notcontains $.Extension) ...
    + ~
    Missing closing '}' in statement block.
    At line:3 char:38
    + Where-Object { (-not $.PSIsContainer) -and ($Exclude -notcontains $.Extension) ...
    + ~
    Unexpected token ')' in expression or statement.
    At line:3 char:67
    + Where-Object { (-not $.PSIsContainer) -and ($Exclude -notcontains $.Extension) ...
    + ~
    You must provide a value expression following the '-notcontains' operator.
    At line:3 char:68
    + Where-Object { (-not $.PSIsContainer) -and ($Exclude -notcontains $.Extension) ...
    + ~~~~~~~~~~~
    Unexpected token '$.Extension' in expression or statement.
    At line:3 char:67
    + Where-Object { (-not $.PSIsContainer) -and ($Exclude -notcontains $.Extension) ...
    + ~
    Missing closing ')' in expression.
    At line:3 char:79
    + Where-Object { (-not $.PSIsContainer) -and ($Exclude -notcontains $.Extension) ...
    + ~
    Unexpected token ')' in expression or statement.
    At line:3 char:81
    + ... s $.Extension) }
    + ~
    Unexpected token '}' in expression or statement.
    + CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : MissingExpressionAfterOperator[/blockquote]
    I'm really not sure what is wrong with my statement.

    Any help would be appreciated
    Tony

  • #20839
    Profile photo of Tony Wainwright
    Tony Wainwright
    Participant

    Please ignore, should be { (-not $_.PSIsContainer) -and ($Exclude -notcontains $_.Extension) }

  • #20840
    Profile photo of Bob McCoy
    Bob McCoy
    Participant

    Keep in mind if you are using PS v3 or later you can simplify your where-object block.

    $Exclude = Get-Content C:\Ephemeral\ExcludeExt.txt
    Get-ChildItem -File -Recurse | where { $PSItem.Extension -notin $Exclude }

You must be logged in to reply to this topic.