1

I am working on using powershell to find all files in a directory input by the user with a string input by the user. Right now if you run my script, and enter in a file path with no sub-folders, then the script works fine, but if I add a folder with sub-folders then it gives me an access denied error. I have tried a few different ways to code it but have had no success. I need it to be able to search the parent directory as well as the child folders. This is what I have right now.

#Sets the ability to execute the script
Set-ExecutionPolicy RemoteSigned

#Requests the loctation of the files to search
$Location = Read-Host 'What is the folder location of the files you want to search?'

#Sets the location based off of the above variable
Set-Location $Location 

#Sets the alias
Set-Alias ss Select-String 

#Requests the text to search for in the files
$File_Name = Read-Host 'Object Name?'

#Searches the files and returns the filenames
ss $File_Name * | Format-List FileName

#Sets location back to root
Set-Location C:

Write-Host "Press any key to continue ..."

$x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
0

2 Answers 2

5

Do not specify folders to Select-String. Replace this line:

ss $File_Name * | Format-List FileName

with

Get-ChildItem -r | Where {!$_.PSIsContainer} | ss $File_Name

On V3 or higher you can simplify to:

Get-ChildItem -r -file | ss $File_Name
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Keith, this worked like a champ. I had similar code without the 'Where' in it, that didn't work for me.
-1

My Script is dynamic and works to find specific file in 3 000 000, and commented part to delete a writhe in a log file.

Param ( #Define o parametro do ano a eliminar "2020" $DateDel = '2018', $DateDe2 = '2019', $DateDe3 = '2020',

 #Define o parametro do registro do ficheiro "_800" ou "_800sm" ou "_200"
 $ResFile1 = '_200',
 $ResFile2 = '_800',
 $ResFile3 = '_800sm',

 #Define o parametro da terminacao do ficheiro "_800.jpg" ou "_800sm.jpg" ou "_200.jpg"
 $TypeFile = '.jpg',
 
 #Define o parametro de onde se localizado ficheiro "C:\users\Luis.Cunha\Desktop\LuisCunha\TarefaScript\TesteFinal\TesteScript1"
 $HomePath = 'C:\Users\Luis.Cunha\Desktop\LuisCunha\TarefaScript\TesteFinal1'   

)

 #Inicia transcriçao de toda informação para o ficheiro .log indicado
 Start-Transcript -Path $HomePath\CountDelItems.log -NoClobber -Append

 Get-ChildItem $HomePath -Recurse -File | Measure-Object | %{$_.Count}

 #o Get vai buscar o ficheiro com a data e a terminacao definidas no $homepath e $tipofich atraves do caminho indicado no $path
 #depois confirma os valores que foram removidos com o verbose
Get-Childitem -Path $HomePath -Recurse -force | Where-Object { !$_.PSIsContainer -and $_.name -like "????$DateDel*$ResFile1$TypeFile" } | Measure-Object | %{$_.Count}
    
 #Get-Childitem -Path $HomePath -Recurse -force | Where-Object { !$_.PSIsContainer -and $_.name -like "????$DateDel*$ResFile1$TypeFile" } | Remove-Item -Verbose -Force 
 
Get-Childitem -Path $HomePath -Recurse -force | Where-Object { !$_.PSIsContainer -and $_.name -like "????$DateDel*$ResFile2$TypeFile" } | Measure-Object | %{$_.Count}

 #Get-Childitem -Path $HomePath -Recurse -force | Where-Object { !$_.PSIsContainer -and $_.name -like "????$DateDel*$ResFile2$TypeFile" } | Remove-Item -Verbose -Force
 
Get-Childitem -Path $HomePath -Recurse -force | Where-Object { !$_.PSIsContainer -and $_.name -like "????$DateDel*$ResFile3$TypeFile" } | Measure-Object | %{$_.Count} 

 #Get-Childitem -Path $HomePath -Recurse -force | Where-Object { !$_.PSIsContainer -and $_.name -like "????$DateDel*$ResFile3$TypeFile" } | Remove-Item -Verbose -Force 

Get-Childitem -Path $HomePath -Recurse -force | Where-Object { !$_.PSIsContainer -and $_.name -like "????$DateDe2*$ResFile1$TypeFile" } | Measure-Object | %{$_.Count}

 #Get-Childitem -Path $HomePath -Recurse -force | Where-Object { !$_.PSIsContainer -and $_.name -like "????$DateDe2*$ResFile1$TypeFile" } | Remove-Item -Verbose -Force 

Get-Childitem -Path $HomePath -Recurse -force | Where-Object { !$_.PSIsContainer -and $_.name -like "????$DateDe2*$ResFile2$TypeFile" } | Measure-Object | %{$_.Count}

 #Get-Childitem -Path $HomePath -Recurse -force | Where-Object { !$_.PSIsContainer -and $_.name -like "????$DateDe2*$ResFile2$TypeFile" } | Remove-Item -Verbose -Force 

Get-Childitem -Path $HomePath -Recurse -force | Where-Object { !$_.PSIsContainer -and $_.name -like "????$DateDe2*$ResFile3$TypeFile" } | Measure-Object | %{$_.Count}

 #Get-Childitem -Path $HomePath -Recurse -force | Where-Object { !$_.PSIsContainer -and $_.name -like "????$DateDe2*$ResFile3$TypeFile" } | Remove-Item -Verbose -Force

Get-Childitem -Path $HomePath -Recurse -force | Where-Object { !$_.PSIsContainer -and $_.name -like "????$DateDe3*$ResFile1$TypeFile" } | Measure-Object | %{$_.Count}

 #Get-Childitem -Path $HomePath -Recurse -force | Where-Object { !$_.PSIsContainer -and $_.name -like "????$DateDe3*$ResFile1$TypeFile" } | Remove-Item -Verbose -Force 

Get-Childitem -Path $HomePath -Recurse -force | Where-Object { !$_.PSIsContainer -and $_.name -like "????$DateDe3*$ResFile2$TypeFile" } | Measure-Object | %{$_.Count}

 #Get-Childitem -Path $HomePath -Recurse -force | Where-Object { !$_.PSIsContainer -and $_.name -like "????$DateDe3*$ResFile2$TypeFile" } | Remove-Item -Verbose -Force 

Get-Childitem -Path $HomePath -Recurse -force | Where-Object { !$_.PSIsContainer -and $_.name -like "????$DateDe3*$ResFile3$TypeFile" } | Measure-Object | %{$_.Count}

 #Get-Childitem -Path $HomePath -Recurse -force | Where-Object { !$_.PSIsContainer -and $_.name -like "????$DateDe3*$ResFile3$TypeFile" } | Remove-Item -Verbose -Force

 

 
 Get-ChildItem $HomePath -Recurse -File | Measure-Object | %{$_.Count}


 #Termina transcrição
 Stop-Transcript 

1 Comment

Please read and understand the question before attempting to respond. As with your other answer, dumping a script that hasn't been adapted at all to address the question isn't helpful.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.