Get mutilple files ext using Get-ChildItem c:\*\*\*

Welcome Forums General PowerShell Q&A Get mutilple files ext using Get-ChildItem c:\*\*\*

This topic contains 2 replies, has 3 voices, and was last updated by

5 years, 2 months ago.

  • Author
  • #11315

    Points: 0
    Rank: Member

    Shall I get multiple files ext using the below command?

    Get-ChildItem c:\*\*\*

    getting below error.
    PS C:\> $exts = ".txt", ".evt", ".jpg", ".png"
    PS C:\> Get-ChildItem c:\*\*\* $exts
    Get-ChildItem : Cannot convert 'System.Object[]' to the type 'System.String' required by parameter 'Filter'. Specified
    method is not supported.
    At line:1 char:24
    + Get-ChildItem c:\*\*\* $exts
    + ~~~~~
    + CategoryInfo : InvalidArgument: (:) [Get-ChildItem], ParameterBindingException
    + FullyQualifiedErrorId : CannotConvertArgument,Microsoft.PowerShell.Commands.GetChildItemCommand

  • #11316

    Points: 1,785
    Helping HandTeam Member
    Rank: Community Hero

    No. The -Path parameter does not accept wildcards in that fashion.

    Look into the -recurse parameter.

  • #11380

    Points: 0
    Rank: Member

    Don is correct in that you should check out the -recurse to look through the entire drive. You should also use -include to include only those file types you're looking for.

    Here's an example of EXcluding certain file types from a search. (This particular script looks for and removes files older than 6 years, excluding certain file types)

    The -Whatif at the end tells powershell to only simulate the command, and not actually remove any files. I generally use that when I am testing scripts that delete data.

    $strDRV = Read-Host 'Enter Drive to Remove Aged Data From: Ex: C:\ '
    Get-ChildItem $strDRV -exclude *.exe,*.mdb,*.dbf,*.ndx -recurse| where {!$_.PSIsContainer -and $_.Lastwritetime -lt (date).adddays(-2190)}|remove-item  -force -WhatIf 

The topic ‘Get mutilple files ext using Get-ChildItem c:\*\*\*’ is closed to new replies.