Powershell 1.0 - Iterate through folders with a ps1 file
How would I iterate through folders with a .ps1 file? I have

    C:\inetpub\wwwroot\apple\login.aspx
    C:\inetpub\wwwroot\orange\login.aspx
    C:\inetpub\wwwroot\banana\login.aspx
    C:\inetpub\wwwroot\pear\login.aspx

I just came across this example:

    # PowerShell Foreach File Example
    Clear-Host
    $Path = "C:\Windows\System32\*.dll"
    Get-ChildItem $Path | Foreach-Object {
    Write-Host $_.Name
    }

So I would do:

    # PowerShell Foreach File Example
    Clear-Host
    $Path = "C:\Windows\System32\*.dll" //here is where I'm not sure of what to do
    Get-ChildItem $Path | Foreach-Object {
    //insert stuff
    }

In my "not sure of what to do" comment how would I change it so it would loop through different sub-directories of windows? Or back to the beginning, loop through each fruit?
May 21st, 2014 4:41pm

Do you want to look for dll files in C:\Windows\System32 or fruit-named aspx files in C:\Inetpub\wwwroot?


Free Windows Admin Tool Kit Click here and download it now
May 21st, 2014 4:45pm

I see your point of confusion and apologize. I was using the C:\Windows example as an example of what I want to do and to show what I've found so far to do what's closest to what I want.  I want to look for aspx files in the different fruit folders, as shown.
May 21st, 2014 4:48pm

Hi,

This will find all aspx files under your fruit folders:

Get-ChildItem 'C:\inetpub\wwwroot\*\*.aspx' | ForEach {

    $_.FullName

}

All the loop does at this point in time is display the full path of the file.

Free Windows Admin Tool Kit Click here and download it now
May 21st, 2014 4:55pm

So the key is to use the "*". And if I want to look for login.aspx I'd just replace "*.aspx" with "login.aspx"?
May 21st, 2014 4:57pm

Yep.
Free Windows Admin Tool Kit Click here and download it now
May 21st, 2014 4:59pm

Makes sense. I'll try it out. 

Thanks!

May 21st, 2014 5:07pm

OK. It works, but only for the first file. I have:

$LoginFolderPath = "C:\inetpub\wwwroot\*\login.aspx";

$LoginFile = $(get-childitem "$LoginFolderPath");
foreach($file in $LoginFile){
  (Get-Content "login.aspx") | ForEach-Object{$_ -replace "<title>Welcome</title>","<title>Welcome to Synergy!</title>"} | Set-Content "login.aspx"
}

It changes the first login.aspx which is in "C:\inetpub\wwwroot\". However, it does not change the other login.aspx in the subdirectories. I think I understand why - because the "Get-Content" in the foreach loop asks for login.aspx and not like it says in $LoginFolderPath.

How do I fix it? Can I do "(Get-Content "$LoginFolderPath")...."?

Just tried the "(Get-Content "$LoginFolderPath") and it did not work

It's not going into the subdirectories it looks like. I moved the login.aspx from C:\inetpub\wwwroot\ into a test folder, so no login.aspx was in it. I then ran it and nothing happened to the file. So what's going on?

Free Windows Admin Tool Kit Click here and download it now
May 21st, 2014 5:59pm

Hi,

Try it this way:

Get-ChildItem 'C:\inetpub\wwwroot\*\login.aspx' | ForEach {
    
    (Get-Content $_.FullName) | ForEach {
    
        $_ -replace "<title>Welcome</title>","<title>Welcome to Synergy!</title>"
        
    } | Set-Content $_.FullName

}

This keeps everything in the pipeline.

May 21st, 2014 9:41pm

Still does not work
Free Windows Admin Tool Kit Click here and download it now
May 22nd, 2014 10:36am

When you say that something didn't work, you have to say how it didn't work.

May 22nd, 2014 10:42am

Still does not work

How so? Worked just fine in my testing.
Free Windows Admin Tool Kit Click here and download it now
May 22nd, 2014 10:44am

I would assume it'd be obvious on how it did not work. I replied to his last suggestion, which one would assume I tried his way and we're still talking about my issue of not replacing text in an aspx are we not? Or should I repeat myself every time with full explanation of what is happening?
May 22nd, 2014 10:48am

How'd you test it? I'll try a different type of test. And you're using powershell 1.0 right? I may have figured out my issue but let me see if I'm right.

Free Windows Admin Tool Kit Click here and download it now
May 22nd, 2014 10:49am

Remember: we can't see your screen. So when you say, "It didn't work," it conveys no actionable information.
May 22nd, 2014 10:51am

How'd you test it? I'll try a different type of test. And you're using powershell 1.0 right? I may have figured out my issue but let me see if I'm right.

I created your folder structure and dummy files for testing.

Also, almost no one is using PowerShell 1.0 any longer. Minimum version you'll find now is 2.0. I'm runnin

Free Windows Admin Tool Kit Click here and download it now
May 22nd, 2014 10:55am

I just solved it and it works. I wasn't putting in the full path: C:\documents and settings......\inetpub\wwwroot\*\login.aspx
May 22nd, 2014 10:59am

I have another related question. Could I set the "<title>Welcome</title>" as a variable and the ""<title>Welcome to Synergy!</title>" as a variable. So for example:

$variable1 = "<title>Welcome</title>"

$variable2 = "<title>Welcome to Synergy!</title>"

Get-ChildItem 'C:\inetpub\wwwroot\*\login.aspx' | ForEach {

(Get-Content $_.FullName) | ForEach { $_ -replace $variable1,$variable2 } | Set-Content $_.FullName }

??

Free Windows Admin Tool Kit Click here and download it now
May 22nd, 2014 11:17am

I don't see why not. Have you tried it?
May 22nd, 2014 11:19am

I'm in the process. I know to test it myself but I thought I'd post this question in case there was an obvious no, and then I would ask why etc etc. 

I tried it and it did work.

Free Windows Admin Tool Kit Click here and download it now
May 22nd, 2014 11:20am

How'd you test it? I'll try a different type of test. And you're using powershell 1.0 right? I may have figured out my issue but let me see if I'm right.

May 22nd, 2014 5:48pm

I'm in the process. I know to test it myself but I thought I'd post this question in case there was an obvious no, and then I would ask why etc etc. 

I tried it and it did work.

Free Windows Admin Tool Kit Click here and download it now
May 22nd, 2014 6:20pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics