We help IT Professionals succeed at work.

Access is Denied Powershelll Select String

I am trying to run a script in PowerShell to look for a string and I am getting an Access is Denied error.  I started PowerShell clicking "Run as Administrator"
The script I am trying to run is

$Location = "C:\Web Sites"
Select-String -path $Location -pattern "H:\\"

How can I get this to run?
Watch Question

Have you changed your execution policy?
Set-ExecutionPolicy RemoteSigned

and then re-run your script


I had not done that.  I just did but I am still getting Access Denied.

See below.

PS C:\Windows\system32> Set-ExecutionPolicy RemoteSigned
Execution Policy Change
The execution policy helps protect you from scripts that you do not trust. Changing the execution policy might expose
you to the security risks described in the about_Execution_Policies help topic. Do you want to change the execution
[Y] Yes  [N] No  [S] Suspend  [?] Help (default is "Y"): Y
PS C:\Windows\system32> $Location = "C:\Web Sites"
PS C:\Windows\system32> Select-String -path $Location -pattern "H:\\"
Select-String : The file C:\Web Sites can not be read: Access to the path 'C:\Web Sites' is denied.
At line:1 char:14
+ Select-String <<<<  -path $Location -pattern "H:\\"
    + CategoryInfo          : InvalidArgument: (:) [Select-String], ArgumentException
    + FullyQualifiedErrorId : ProcessingFile,Microsoft.PowerShell.Commands.SelectStringCommand
How about try this:
Select-String -Path "C:\Web Sites\*.*" -pattern "H:\\"

Open in new window


That is helpful - I am no longer getting an error but I am not finding values that I no exist.

I am trying to search through text files for any instance of "H:\" but this isn't finding it.  Do I have a formatting problem with my pattern?


This seems to work
GET-CHILDITEM "C:\Web Sites\*" -recurse | SELECT-STRING -pattern "H:\\" | out-file "C:\outfile.txt"

Open in new window

Hmm. that's interesting. what you did will work as well.  
When I tried the first one on my workstation, it found the value in multiple files.
As long as you got pointed in the right direction, it's all well.



The files with the text were in sub-folders of the folder I had in the path - that may have been an important piece of information I neglected to share.  :-)

Thanks for your help!
Minor detail! :)
You're welcome!