Author Posts

November 27, 2014 at 7:33 am

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

November 27, 2014 at 7:37 am

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

November 27, 2014 at 9:58 am

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 }