user630702
user630702

Reputation: 3097

Powershell - Get-ChildItem wildcard

Get-ChildItem filename*.log.* fetches filename*.log as well. How can I get only the log files ending with dot extension filename*.log.* so I can delete them? I want to use Remove-Item but decided to check using get-childItem.

Here is the files.

Server1234.log
Server1234.log.1
Server1234.log.2
Server1234.log.3
Server1234.log.4

Get-ChildItem filename*.log.* shows all of the above. I don't want Server1234.log in the output.

Upvotes: 1

Views: 2193

Answers (2)

Martin Brandl
Martin Brandl

Reputation: 58931

Your filter should work. I have created 3 files:

New-Item 'Server1234.log' -ItemType File
New-Item 'Server1234.log.1' -ItemType File
New-Item 'Server1234.log.2' -ItemType File

And here is the output of Get-ChildItem Server1234*.log.*

PS D:\> Get-ChildItem Server1234*.log.*

    Directory: D:\


Mode                LastWriteTime         Length Name                                                                                                                       
----                -------------         ------ ----                                                                                                                       
-a----       13/05/2020     10:51              0 Server1234.log.1                                                                                                           
-a----       13/05/2020     10:51              0 Server1234.log.2 

Note: The filter parameter of the Get-ChildItem cmdlet doesn't use regex! If you want to use regex you can do this within the Where-Object cmdlet:

Get-ChildItem | Where-Object { $_.Name -match 'Server123.*log\.+' }

Upvotes: 4

Peter Schneider
Peter Schneider

Reputation: 2929

You might use the filter

Get-ChildItem filename*.log.?*

The question mark states that at least one character has to be there...

Upvotes: 1

Related Questions