0

I am writing a PowerShell script to get all the items in a particular folder path specified. I am using following command :

$fileName= Get-ChildItem -Path "C:\Path\to\Bin"  -Force -Include User.Base.Tests.*.dll

I am not using -Recurse but it is returning empty results. I want result from only Bin folder not from sub folder. If I user -Recurse command it will give me results from sub folders as well which I don't want. What am I missing here?

NetSurfer
  • 103
  • 1
  • 9
  • You are just asking for a list of files with that wildcard. There is no real reason to use -include for that. Can you state your reason for deciding to use -Include for a this? – postanote Apr 04 '20 at 08:06

2 Answers2

2

We have to include a wildcard character in the path as below:

Get-ChildItem -path "C:\Path\to\Bin\*" -Force -Include User.Base.Tests.*.dll

The Include parameter is effective only when the command includes the Recurse parameter or the path leads to the contents of a directory, such as C:\Windows, where the wildcard character specifies the contents of the C:\Windows directory

See: Confused with -Include parameter of the Get-ChildItem cmdlet

Itchydon
  • 2,572
  • 6
  • 19
  • 33
0

Without the -Include the below works as expected. However, with a file name like that, you want to quote stuff with those constructs. IMHO.

Get-ChildItem -Path D:\temp User.Base.Tests.*.dll | 
Format-Table -AutoSize
<#
# Results

    Directory: D:\temp


Mode          LastWriteTime Length Name                 
----          ------------- ------ ----                 
-a----  04-Apr-20     00:45      0 User.Base.Tests.1.dll
-a----  04-Apr-20     00:45      0 User.Base.Tests.2.dll
#>


Get-ChildItem -Path 'D:\temp' 'User.Base.Tests.*.dll' | 
Format-Table -AutoSize
<#
# Results

    Directory: D:\temp


Mode          LastWriteTime Length Name                 
----          ------------- ------ ----                 
-a----  04-Apr-20     00:45      0 User.Base.Tests.1.dll
-a----  04-Apr-20     00:45      0 User.Base.Tests.2.dll
#>
postanote
  • 15,138
  • 2
  • 14
  • 25