I'm trying to get all the files that end with ".asp" but I also get the files that ends in ".aspx" or ".asp..." when I do :
Get-ChildItem C:\test -Filter *.asp -Recurse | % {$_.FullName}
For the example, let's say I have a directory with test1.asp, test2.aspx and test3.asp, if I execute my script, the output will be:
C:\test\test1.asp
C:\test\test2.aspx
C:\test\test3.asp
but I only wanted it to get me "test1.asp and test3.asp".
For information, I use Powershell 2.1.
Can someone tell me how to fix that?
Try to check one more the last 3 symbols
Get-ChildItem 'C:\test' -Filter '*.asp' -Recurse |
Where {$_.Name.substring($_.Name.length -3, 3) -Match 'asp'} | % {$_.FullName}
Caveat: PowerShell behavior seems to have changed!
Windows PowerShell 5.1.19041.2364:
PS C:\Users\OA> Get-ChildItem -File -Path "C:\Test" -Filter "*.txt"
Verzeichnis: C:\Test
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 05.02.2023 09:59 0 TestFile.txt
-a---- 05.02.2023 09:59 0 TestFile.txtLirumLarumLöffelstiel
PowerShell Core 7.3.2:
PS C:\Users\OA> Get-ChildItem -File -Path "C:\Test" -Filter "*.txt"
Directory: C:\Test
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a--- 05.02.2023 09:59 0 TestFile.txt
Both commands were performed on Microsoft Windows Pro 10.0.19045.2546 (22H2).
Behavior on legacy operating systems: PS version 2.0 on Windows XP (using wildcard path instead of -File parameter) and PS version 5.14409.1018 on Windows 7: both have the former behavior.
Related
I have written a simple script that should collect all local documents from folders on C: drive (*.pdf, *.jpg, *.xls, *.doc) and save them to the "computername" text file under the C:\TEMP folder.
I have been able to launch this locally on Windows 11 machine without issues! When I tried to do the same thing on Windows 10 machine text file was empty!
I have narrow this down to the user account interaction, as it seems on Windows 10 machines. If this script is launched from PowerShell running as current user on Windows 10 it seems the script generates output. If it is Run as Administrator (locally or via remote push as admin) I got empty file. I would appreciate any guidance.
code:
$FindDate=(Get-Date).adddays(-180)
[System.IO.Directory]::CreateDirectory('C:\TEMP')
{Get-ChildItem -Path C:\ -Directory} |
Where-Object Name -NotIn #('Windows','Program Files','$Recycle.Bin','Windows.old') |
% { Get-ChildItem -Include *.doc,*.docx,*.pdf,*.jpg,*jpeg,*.xls,*.xlsx -File -
Recurse} -ErrorAction SilentlyContinue |
Where-Object { $_.LastWriteTime -ge $FindDate } |
Select LastWriteTime,Name,Directory |
Out-File C:\TEMP\$env:Computername.txt
there are some issues with the current code:
e.g. you do:
{Get-ChildItem -Path C:\ -Directory}
this defines a expression and will not execute:
PS C:\Users\vcxy> {Get-ChildItem -Path C:\ -Directory}
Get-ChildItem -Path C:\ -Directory
PS C:\Users\vcxy>
you want probably:
PS C:\Users\vcxy> Get-ChildItem -Path C:\ -Directory
Directory: C:\
Mode LastWriteTime Length Name
---- ------------- ------ ----
d----- 8/15/2022 2:46 PM AMD
d----- 6/5/2021 2:10 PM PerfLogs
d----- 4/19/2022 6:56 PM plc_debug
d-r--- 9/22/2022 6:26 PM Program Files
d-r--- 6/12/2022 5:02 PM Program Files (x86)
d----- 9/27/2022 6:43 PM TEMP
d----- 9/27/2022 5:06 PM tmp
d-r--- 10/6/2021 12:44 PM Users
d----- 4/8/2020 2:18 PM VM
d----- 9/22/2022 10:46 PM Windows
I'll updated your version as needed:
$FindDate=(Get-Date).adddays(-180)
#This works but you could also do new-item C:\temp -ItemType Directory
[System.IO.Directory]::CreateDirectory('C:\TEMP')
$folders = Get-ChildItem -Path C:\ -Directory | Where-Object {$_.Name -Notmatch 'Windows|Program Files|$Recycle\.Bin|Windows\.old'}
$files = #(
$folders | % {
Get-ChildItem -Path $_.fullname -Include *.doc,*.docx,*.pdf,*.jpg,*jpeg,*.xls,*.xlsx -File -Recurse -ErrorAction:SilentlyContinue | Where-Object {$_.LastWriteTime -ge $FindDate}
}
)
#You should use export-csv, its better for later processing of the data
If ($files){
$files | Select LastWriteTime,Name,Directory | export-csv C:\TEMP\$env:Computername.txt
}
It will only create the output file if files are found, no files no output file or in regards to the original version, no files no content in the output file.
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.
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\.+' }
You might use the filter
Get-ChildItem filename*.log.?*
The question mark states that at least one character has to be there...
I have a PowerShell cmdlet with the following line
$items = Get-ChildItem -Path $FolderName -File -Force |
Sort CreationTime |
Select -First 1 -Last 1
It works fine under my normal login but if I log onto my machine as a domain admin I get an error message telling me that -File is not recognised as a valid parameter for Get-ChildItem.
I suspected that the domain admin was running an earlier version of PowerShell so under both accounts I have run $PSVersionTable.PSVersion and get the following:
Major Minor Build Revision
----- ----- ----- --------
5 0 10586 117
If anything I would expect my local login to fail and the domain admin login to succeed due to permissions differences but it seems to be working the other way around.
Could Get-ChildItem somehow have been overwritten in your $profile or something else?
You can check what Get-ChildItem executes if you run this:
get-command Get-ChildItem
CommandType Name Version Source
----------- ---- ------- ------
Cmdlet Get-ChildItem 3.1.0.0 Microsoft.PowerShell.Management
If it is overwritten by doing something like this:
Function Get-ChildItem { }
Then it will would show this:
get-command Get-ChildItem
CommandType Name Version Source
----------- ---- ------- ------
Function Get-ChildItem
If that would be the case, you can remove the custom version with
Remove-Item Function:\Get-ChildItem
You could also try to not use the -File parameter, and rather filter out folders yourself:
$items = Get-ChildItem -Path $FolderName -Force |
Where PSIsContainer -eq $False |
Sort CreationTime |
Select -First 1 -Last 1
The problem can be resolved by ensuring that the PATH and PSModulePath variables are set to include the location of the ps1 scripts.
In this particular case the PATH and PSModulePath variable included the scripts location for the 1st user but not the 2nd user.
By correcting this the scripts ran successfully for the 2nd user login.
Can someone please explain the difference between -Include and -Filter options in the Get-ChildItem command .
Below are the two pieces of code that I am trying to execute . They both serve to find out the text files in a particular directory:
PS C:\Users\352997> get-childitem -path Desktop\Extras -filter *.txt
Directory: C:\Users\352997\Desktop\Extras
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a--- 12/22/2014 4:05 PM 140 Expense_report.txt
-a--- 1/14/2015 4:41 PM 211 Extras.txt
-a--- 2/10/2015 2:46 PM 259 Learn Dutch.txt
PS C:\Users\352997> get-childitem -path Desktop\Extras -include *.txt
--The above command produces no result ----
Filter parameter is implemented by provider. It is efficient because applies when retrieving the objects.
Get-PSprovider commandlet shows providers that implement 'filter' parameter. For example, there are only two providers on my
system:ActiveDirectory and FileSystem
Include parameter is implemented by Powershell. It only works in conjunction with Recurse parameter (as MSDN describes here).
It's interesting that:
get-childitem -path Desktop\Extras\ -include *.txt
returns nothing
get-childitem -path Desktop\Extras\* -include *.txt
returns list of *.txt files
Maybe these are just nuances of the implementation.
Also see this excellent blog post: http://tfl09.blogspot.com/2012/02/get-childitem-and-theinclude-and-filter.html
-filter should be faster than -include. -filter can match the short version of filenames in powershell 5.1.
Is there a way to determine how wildcard matching is done in Get-ChildItem?
Various articles (1, 2) suggest that it is done through the WildcardPattern class, but I don’t think this is the case. For example, suppose you have a file in C:\test\test2\testfile.txt. Then Get-ChildItem –Path “C:\*\testfile.txt” will not find the file while WildcardPattern::IsMatch will. Wildcard "*" matching in Get-ChildItem seems to be on directory level: so "\*\" will never match more than one level, like "\A\B\".
So if WildcardPattern class isn't used, then what is?
From what I know, it's using the WildcardPattern as you describe. However, the cmdlet Get-ChildItem limits it to the current directory (characters except \), so it won't conflict with the -Recurse switch that goes to unlimited levels.
With "C:\*\testfile.txt", the asterisk plays a role just for the first level directory (e.g test). The file you're looking for is not there and the output you get is expected. Add another asterisk for the second level and you'll get the desired output (e.g "C:\*\*\testfile.txt"). You can also add the Recurse switch to start searching from the current location, all the way downwards.
Either would work:
gci c:\test\*\testfile.txt
or
gci c:\*\testfile.txt -recurse
Example:
PS C:\temp\test2> dir
Directory: C:\temp\test2
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a--- 4/4/2013 10:41 PM 0 testfile.txt
PS C:\temp\test2> cd \
PS C:\> gci c:\*\testfile.txt -recurse -ea SilentlyContinue
Directory: C:\Temp\test2
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a--- 4/4/2013 10:41 PM 0 testfile.txt