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

This topic contains 2 replies, has 3 voices, and was last updated by Profile photo of notarat notarat 3 years, 5 months ago.

  • Author
    Posts
  • #11315
    Profile photo of Biswajit
    Biswajit
    Participant

    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
    Profile photo of Don Jones
    Don Jones
    Keymaster

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

    Look into the -recurse parameter.

  • #11380
    Profile photo of notarat
    notarat
    Participant

    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 
    

You must be logged in to reply to this topic.