In today post I will show you how to use PowerShell in order to file search. Will use Get-ChildItem command or if some of you like shortcuts – gci. In general this command is used to list objects in some location specified in parameter -Path. That location could be file system or registry.
The simplest usage of that command will be:
1 |
Get-ChildItem C:\Temp |
It list us all files, which are placed directly in temp directory. If we need to search also folders inside folder temp we need to add -Recurse parameter. Using recursive searching we can expect that will not have permissions to some files and then will receive error. To avoid that we only need to add param which is handle this error. Full comman will be like that:
1 |
Get-Childitem –Path C:\temp -Recurse -ErrorAction SilentlyContinue |
In this case if any problem will appear, script will not generate any error and will continue execution.
OK, so how to search files with specified name? Below examples.
If we will add -Recurse will search all folders inside actual folder you are.
There are also available parameters -Include oraz -Exclude. Include is similar to –Filter.
According to documentation if we will not add * in parameter -Path like in the example or if parameter leads to specified path like: -Path C:\Temp\* this command will not show any results. When using -Recurse then * is optional to use.
1 2 3 |
PS C:\Temp> Get-ChildItem -Include *.txt PS C:\Temp> Get-ChildItem * -Include *.txt |
Now lets try to show how to filter results excluding some files from. First, search files but without txt files.
1 |
Get-ChildItem -Exclude *.txt |
Now, lets exclude all files where name is starting from „t”.
1 |
Get-ChildItem * -Exclude t* |
As mentioned at beggining Get-ChildItem could be used also to registry.
1 |
Get-ChildItem -Path HKLM:\SOFTWARE |