1

I want to search the content of specific folder for Web.config files. Here's the script I'm using but it doesn't work and don't know how to tweak it.

$folders=get-childitem E:\WebSystems\Configs\ | select Name
foreach($folder in $folders)
{
    get-content E:\WebSystems\Configs\$folder\Web.config
}
Mikko Viitala
  • 8,344
  • 4
  • 37
  • 62
user3911596
  • 35
  • 1
  • 3
  • 7

3 Answers3

3

Please try the following code ,

$folders=get-childitem E:\WebSystems\Configs\ | select Name
foreach($folder in $folders)
{
$folderName = $folder.Name;
get-content E:\WebSystems\Configs\$folderName\Web.config
}

And please ensure the Folder *E:\WebSystems\Configs* has only subfolders and no files. If you are still facing error, please post the exact error details.

Godwin
  • 600
  • 5
  • 16
  • You're the man! I also just found another way: get-childitem E:\WebSystems\Configs\ | Foreach-Object{get-content E:\WebSystems\Configs\$_\Web.config} – user3911596 Aug 05 '14 at 19:27
  • By the way, how to do you replace a string to those individual files? – user3911596 Aug 05 '14 at 20:16
  • Thanks , I just found something simliar in [this thread](http://stackoverflow.com/questions/17144355/string-replace-file-content-with-powershell) , see if this works for you. – Godwin Aug 05 '14 at 20:23
0

How about

get-childitem E:\WebSystems\Configs\* -include Web.Config -recurse

What do you want to do with the collection of Web.Config files? (Get-Content probably isn't very helpful because the output won't tell you which files is in which directory.)

Bill_Stewart
  • 22,916
  • 4
  • 51
  • 62
0

You can just do

get-content E:\WebSystems\Configs\*\Web.config

to get an array of all Web.config files. If you want to iterate over each file you can do

get-childitem E:\WebSystems\Configs\*\Web.config | foreach{
  get-content $_.fullname }

For seaching you can then use

Get-Help Select-String
fbehrens
  • 6,115
  • 2
  • 19
  • 22