Powershell accessing files with select-string - access is denied Powershell accessing files with select-string - access is denied powershell powershell

Powershell accessing files with select-string - access is denied


To expand on the comment from @LotPings You could get just the files in D:\myfolder by using -File parameter from Get-ChildItem. This way you will not pass in a directory to Select-String.

$input_path = 'd:\myfolder'$Files = Get-ChildItem $input_path -File | Select-Object -ExpandProperty FullNameForeach ($File in $Files) {    $output_file = 'd:\extracted_URL_addresses.txt'    $regex = '([a-zA-Z]{3,})://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)*?'    select-string -Path $file -Pattern $regex -AllMatches | % { $_.Matches } | % { $_.Value } > $output_file}


  • As $input is an automatic variable I wouldn't use it - not even as part of a variable name.
  • You don't need two stacked ForEach-Object use $_.Matches.Values instead
  • Using a file extension in the path could eventually avoid the error
  • Using the folllowing script on a copy of this webpage works flawlessly but has a lot of dupes, so I'd append a |Sort-Object -Unique

$FilePath = '.\*.html'$OutputFile = '.\extracted_URL_addresses.txt'$regex = '([a-zA-Z]{3,})://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)*?'Get-ChildItem -File $FilePath |  Select-String -Pattern $regex -AllMatches |     ForEach-Object { $_.Matches.Value } |Sort -Unique > $OutputFile