Question: I want to exclude all directories from my search in PowerShell. Both `FileInfo` and `DirectoryInfo` contain `Attributtes` property that seems to be exactly what I want, but I wasn't able to find out how to filter based on it. Both

```
ls | ? { $_.Attributes -ne 'Direcory' }
ls | ? { $_.Attributes -notcontains 'Direcory' }

```

didn't work. How can I do this?

Answer: 
You can use the `PSIsContainer` property:

```
gci | ? { !$_.PSIsContainer }

```

Your approach would work as well, but would have to look like this:

```
gci | ? { !($_.Attributes -band [IO.FileAttributes]::Directory) }

```

as the attributes are an enum and a bitmask.

Or, for your other approach:

```
gci | ? { "$($_.Attributes)" -notmatch "Directory" }

```

This will cause the attributes to be converted to a string (which may look like "Directory, ReparsePoint"), and on a string you can use the `-notmatch` operator.

PowerShell v3 finally has a `-Directory` parameter on `Get-ChildItem`:

```
Get-ChildItem -Directory
gci -ad

```
### Exclude directories in PowerShell:

```
Get-ChildItem | Where-Object {$_ -isnot [IO.DirectoryInfo]}

```

Or it's terse, but harder to read version:

```
gci | ? {$_ -isnot [io.directoryinfo]}

```

Credit goes to @Joey for his insightful comment using the `-is` operator :)

### However

Technically, I prefer *including* only Files or only Directories since excluding can lead to unexpected results as Get-ChildItem can return more than just files and directories :)

### Include just Files:

```
Get-ChildItem | Where-Object {$_ -is [IO.FileInfo]}

```

Or:

```
gci | ? {$_ -is [io.fileinfo]}

```

### Include just Directories:

```
Get-ChildItem | Where-Object {$_ -is [IO.DirectoryInfo]}

```

Or:

```
gci | ? {$_ -is [io.directoryinfo]}

```