In Powershell, we can use Get-ChildItem cmdlet to search for files. But it doesn't give you all the fun. So I have developed Find-ChildItem Powershell cmdlet which is equivalent to Unix find command.
Download Find-ChildItem for both 32 bits and 64 bits |
Note: For 64 bit operating system, you will have to use different InstallUtil.exe. See below for details
Syntax:
Find-ChildItem [-Name <String>] [-Type <String>] [-MaxDepth <Int32>] [-MinDepth <Int32>] [-CMin <String>]
[-AMin <String>] [-WMin <String>] [-CTime <String>] [-ATime <String>] [-WTime <String>] [-Size <String>]
[-Empty] [-Exec <String>] [-Delete] [-OutObject] [[-Path] <String>] [<CommonParameters>]
Parameter | Description |
Name | Accepts .Net regular expression to match FileName |
Type | d - directory, f - file |
MaxDepth | Descend at most levels (a non-negative integer) levels of directories below the command line arguments |
MinDepth | Do not apply any tests or actions at levels less than specified mindepth level (a non-negative integer) |
CMin | File was created n minutes ago |
CTime | File was created n*24 hours ago |
AMin | File was last accessed n minutes ago |
ATime | File was last accessed n*24 hours ago |
WMin | File was last changed/written n minutes ago |
WTime | File was last changed/written n*24 hours ago |
Size | File uses n units of space |
Empty | File is empty |
Exec | Execute Cmdlets in the given argument. |
Delete | Delete file or directory |
OutObject | Write Objects in output instead of just Directory/File Names |
Find-ChildItem Cmdlet Installation Instructions:
- Extract Find-ChildItem.zip files to C:\Windows\Microsoft.NET\Framework\ Directory
- Open Powershell with Administrator Privileges
- Now, lets install the cmdlet using InstallUtil.exe utility which is available as part of Microsoft Windows SDK.
For 32 Bit Operating System:C:\Windows\Microsoft.NET\Framework\v2.0.50727\InstallUtil.exe "C:\Windows\Microsoft.NET\
For 64 Bit Operating System:
Framework\Find-ChildItem.dll"C:\Windows\Microsoft.NET\Framework64\v2.0.50727\InstallUtil.exe "C:\Windows\Microsoft.NET\
Framework\Find-ChildItem.dll" - Type Get-PSSnapin -Registered and press enter. If it displays FindChildItemPSSnapIn01 SnapIn, then our installation is successful
- Now, before we can use Find-ChildItem Cmdlet, we need to add FindChildItemPSSnapIn01 using Add-PSSnapin FindChildItemPSSnapIn01
- To permanently add the Snapin, Add-PSSnapin FindChildItemPSSnapIn01 Cmdlet need to be added to Windows Powershell Profile file
- See the below window, to get clear picture on Cmdlet Installation on Windows 7 32 Bits
PS C:\Users\Jagadish\> cd "C:\Windows\Microsoft.NET\Framework"
PS C:\Windows\Microsoft.NET\Framework> Get-ChildItem .\Find-ChildItem*
Directory: C:\Windows\Microsoft.NET\Framework
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a--- 6/8/2009 1:13 PM 12288 Find-ChildItem.dll
-a--- 6/8/2009 1:14 PM 21298 Find-ChildItem.dll-Help.xml
PS C:\Windows\Microsoft.NET\Framework> C:\Windows\Microsoft.NET\Framework\v2.0.50727\InstallUtil.exe .\Find-ChildItem.dll
Microsoft (R) .NET Framework Installation utility Version 2.0.50727.3521
Copyright (c) Microsoft Corporation. All rights reserved.
Running a transacted installation.
.
.
.
The Commit phase completed successfully.
The transacted install has completed.
PS C:\Windows\Microsoft.NET\Framework> Get-PSSnapin -Registered
Name : FindChildItemPSSnapIn01
PSVersion : 2.0
Description : This is Powershell SnapIn that includes Find-ChildItem Cmdlet, developed by Jagadish <http://windows-powershell-scripts.blogspot.com/>
PS C:\Windows\Microsoft.NET\Framework> Add-PSSnapin FindChildItemPSSnapIn01
PS C:\Windows\Microsoft.NET\Framework> Find-ChildItem G:\Jagadish\Cmdlets
G:\Jagadish\Cmdlets\Test
G:\Jagadish\Cmdlets\Test\1
G:\Jagadish\Cmdlets\Test\1\2
G:\Jagadish\Cmdlets\Test\1\file2
If you don't have windows powershell profile file, follow the below steps to create a new one and add "Add-PSSnapin FindChildItemPSSnapIn01" entry to it
PS C:\Users\Jagadish\Desktop\Find-ChildItem> New-Item $profile -ItemType file -Force ; Write-Output "Add-PSSnapin FindChildItemPSSnapIn01" > $profile
Directory: C:\Users\Jagadish\Documents\WindowsPowerShell
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a--- 6/8/2009 6:28 PM 0 Microsoft.PowerShell_profile.ps1
That's it. You added the Add-Snapin entry permanently. Now, Lets have some fun with new Find-ChildItem Cmdlet
List all the files and directories in the current directory
PS G:\Jagadish\Test> Find-ChildItem
G:\Jagadish\Test\Docs
G:\Jagadish\Test\Docs\101_Popular_Interview_Questions.doc
G:\Jagadish\Test\Docs\echosend_1500.c
G:\Jagadish\Test\Downloads
G:\Jagadish\Test\Downloads\VLC
G:\Jagadish\Test\Downloads\VLC\vlc-0.8.6-win32.exe
G:\Jagadish\Test\Downloads\VLC\vlc_src-0.8.5.tar.gz
G:\Jagadish\Test\Downloads\ethereal-setup-0.10.13.exe
G:\Jagadish\Test\Downloads\TeamViewer_Setup.exe
G:\Jagadish\Test\Songs
G:\Jagadish\Test\Songs\Green Day
G:\Jagadish\Test\Songs\Green Day\American Idiot- Boulevard Of Broken Dreams.mp3
G:\Jagadish\Test\Songs\akon-sorry.mp3
G:\Jagadish\Test\msvcp71.dll
PS G:\Jagadish\Test>
List ".exe" files only
PS G:\Jagadish\Test> Find-ChildItem -Type f -Name ".*.exe"
G:\Jagadish\Test\Downloads\VLC\vlc-0.8.6-win32.exe
G:\Jagadish\Test\Downloads\ethereal-setup-0.10.13.exe
G:\Jagadish\Test\Downloads\TeamViewer_Setup.exe
PS G:\Jagadish\Test>
Search for ".c" files only and display no of lines, words, characters in that file
PS G:\Jagadish\Test> Find-ChildItem -Type f -Name "\.c$" -Exec "Get-Content {} | Measure-Object -Line -Character -Word"
Lines Words Characters Property
----- ----- ---------- --------
162 500 3703
PS G:\Jagadish\Test>
You can use the below command to find Empty files in a directory and its sub directories
PS E:\Jagadish\Test> Find-ChildItem -Type f -Empty
E:\Jagadish\Test\Docs\Attachment
E:\Jagadish\Test\Docs\Report.txt
E:\Jagadish\Test\Docs\sample_logs
E:\Jagadish\Test\testing
Use -OutObject parameter to write file objects to powershell
PS E:\Jagadish\Test> Find-ChildItem -Type f -Empty -OutObject
Directory: Microsoft.PowerShell.Core\FileSystem::E:\Jagadish\Test\Docs
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a--- 8/11/2009 10:16 PM 0 Attachment
-a--- 8/11/2009 10:16 PM 0 Report.txt
-a--- 8/11/2009 10:16 PM 0 sample_logs
Directory: Microsoft.PowerShell.Core\FileSystem::E:\Jagadish\Test
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a--- 8/11/2009 10:16 PM 0 testing
Now, If we want to delete all the empty files, we can easily do that with the help of Find-ChildItem cmdlet
Find-ChildItem -Type f -Empty -Delete
PS E:\Jagadish\Test> Find-ChildItem -Type f -Empty -Delete
PS E:\Jagadish\Test> Find-ChildItem -Type f -Empty -OutObject
PS E:\Jagadish\Test>
Now, list all the files with size greater than 9 MB
PS E:\Jagadish\Test> Find-ChildItem -Type f -Size +9M -OutObject
Directory: Microsoft.PowerShell.Core\FileSystem::E:\Jagadish\Test\Downloads\VLC
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a--- 12/13/2006 3:26 PM 9451515 vlc-0.8.6-win32.exe
-a--- 1/25/2007 3:00 PM 81510400 vlc-binary-fc4.tar.gz
-a--- 7/7/2006 5:46 PM 15680081 vlc-binary.tar.gz
-a--- 11/8/2006 5:45 PM 12461262 vlc_src-0.8.5.tar.gz
Directory: Microsoft.PowerShell.Core\FileSystem::E:\Jagadish\Test\Downloads
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a--- 11/9/2006 7:24 PM 11348769 ethereal-setup-0.10.13.exe
If you want more details and usage information on this Find-ChildItem Cmdlet, type Get-Help Find-ChildItem -Full in your Powershell
Let me know if you need any other information or if you encounter any bugs/issues with Find-ChildItem Cmdlet