Powershell looping and launching through folders

Hi,
I am trying to run a Powershell script that will loop through a folder directory which contains sub folders and what I want to do is launch everything within the main folder directory and wait for the user input to continue before launching everything in each sub folders one by one but waiting for the user input before continuing down all the sub folders. So folder A contents are launched, the script waits for the user to say continue, once continue the script launches folder B's contents etc. Once complete down all sub folders, prompt that all files have now been launched.
Sukpal JohalAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ITguy565Commented:
Hmm, well in theory that wouldn't be hard to accomplish.. Do these files need to be launched in any particular order?
0
Thomas UCommented:
Hi

Are the files executables or txtfiles or something?
This shows you the files and waits for keystroke after each file...I need to know what you want to execute

$files = get-ChildItem -Path c:\temp\folder -Recurse -File

foreach($file in $files)
{
	write-host $file
	pause "Press key to continue"
}

Open in new window

1
Thomas UCommented:
Wait...it doesnt matter...

that should work..

$files = get-ChildItem -Path c:\temp\folder -Recurse -File

foreach($file in $files)
{
	start $file
	pause "Press key to continue"
}

Open in new window

1
Challenges in Government Cyber Security

Has cyber security been a challenge in your government organization? Are you looking to improve your government's network security? Learn more about how to improve your government organization's security by viewing our on-demand webinar!

Sukpal JohalAuthor Commented:
Hi, the files within each subfolder and the main folder can be launched in any order but the loop will need to move alphabetically. So Folder A, Folder B etc and not Folder A, Folder C and back to Folder B.
0
Thomas UCommented:
Sorry I think I misinterpreted the question a bit...

You want to execute everything in the mainfolder....THEN ask for continue...THEN exeute everything in the next subfolder, I assume sorted by Name? After EACH subfolder it asks the user to execute everything in the next folder?
0
Sukpal JohalAuthor Commented:
Hi
Thomas U. Close but I don't want to write the name but to launch the files.  I.E. Invoke
0
ITguy565Commented:
That is very close to what I was working on :P

$workingdir = "c:\somepath"

set-location -path $workingDir
$folderNames = Get-ChildItem -Recurse | ?{ $_.PSIsContainer } | Select-Object FullName

foreach ($folder in $foldernames){
    foreach ($file in $folder){
        start $file
    }
    pause "Enter a Key to Continue"
}

Open in new window

0
Sukpal JohalAuthor Commented:
Correct  Thomas U that is exactly what I am after
0
Thomas UCommented:
I think we are near the solution...ITguy565 code is almost working. mine does not refelct the pause between the folders, but we get to that ;)
0
Sukpal JohalAuthor Commented:
ITguy565 this is close but the script actually does not launch anything. I dont think "start" works.  I have changed the directory and tested the path test-path and it was true. So there is an error within this.

Start : This command cannot be run due to the error: The system cannot find the file specified.
At line:10 char:9
+         Start $file
+         ~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [Start-Process], InvalidOperationException
    + FullyQualifiedErrorId : InvalidOperationException,Microsoft.PowerShell.Commands.StartProcessCommand
0
ITguy565Commented:
hmmm.. If you open powershell and just type start into the prompt what does it say?
0
Sukpal JohalAuthor Commented:
I have come close but it launches the folder as well as the file but each user input is made per execution so press enter - folder 1 launches, press enter folder 2 launches, press enter, folder 1 content launches, press enter folder 1 second conent launches, press enter folder 2 content launches.

What I want is, Press Enter, Every file within folder 1 to launch but not the folder. Press enter, every file within folder 2 launch etc.

This is what I have so far:

$path = "C:\Users\Administrator\Downloads\test"

$files = Get-ChildItem $path -Recurse

#Write-Host $files

foreach ($file in $files){
    invoke-item $file.FullName
    Read-Host -Prompt "Press any key to continue"
}
0
Thomas UCommented:
I took ITguy565's code and altered it a little bit. If that works now as a PS1 file below, you can give him the credit ;)

cheers
Thomas

$workingdir = "c:\temp\hash"

set-location -path $workingDir

$folderNames = Get-ChildItem -Recurse | ?{ $_.PSIsContainer }

foreach ($folder in $foldernames){
	$files = get-ChildItem -Path $folder -File | % { $_.FullName }
	write-host $files
	start $files
pause "Enter a Key to Continue"
}

Open in new window


you can remove the "write-host $files" thats just for control
0
Sukpal JohalAuthor Commented:
ITguy565 Start itself works and it asks for the filepath which I enter manually and it launches the folder but within the script it will not launch the files etc.
0
Sukpal JohalAuthor Commented:
Thomas U very close but it has still failed.

Start-Process : Cannot convert 'System.Object[]' to the type 'System.String' required by parameter 'FilePath'. Specified method is not supported.
At line:12 char:8
+     start $files
+           ~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Start-Process], ParameterBindingException
    + FullyQualifiedErrorId : CannotConvertArgument,Microsoft.PowerShell.Commands.StartProcessCommand
0
Thomas UCommented:
it works here without error...
hmm but I think there is still an issue with the root folder....not correct...need more time
0
ITguy565Commented:
Try this
$workingdir = "c:\testdir"

set-location -path $workingDir

$folderNames = Get-ChildItem -Recurse | ?{ $_.PSIsContainer }

foreach ($folder in $foldernames){
    $files = get-ChildItem -Path $folder -File | % { $_.FullName }
        foreach ($file in $files){
	write-host $file
    start $file
        }
pause "Enter a Key to Continue"
}

Open in new window

0
Thomas UCommented:
I think I got it now
@ITguy565 can you test it as well? Seems to work now for me..

$workingdir = "c:\temp\hash"

set-location -path $workingDir


#execute all files within the workingdir, no recurse
$rootfolder = Get-ChildItem -file #| % { $_.FullName }
foreach ($file in $rootfolder){
	start $file
}

# get all folders in $workingdir
$folderNames = Get-ChildItem -Recurse | ?{ $_.PSIsContainer }

#loop through folders and execute, pause after folder
foreach ($folder in $foldernames){
	#get all files in the subfolder with fullpath
	$files = get-ChildItem -Path $folder -File | % { $_.FullName }
		# need loop through all files
		foreach ($fileSub in $files){
	    
		start $fileSub
		}
	# Waits for user to continue
	Write-Host "Press any Key to continue with folder:" $folder
	$x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
	
}

Open in new window

1

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ITguy565Commented:
works for me Thomas..
1
Sukpal JohalAuthor Commented:
Thomas U - Unfortunately it has not worked for me. The files are all launched within every folder and the pause for the user interaction never happens.

Exception calling "ReadKey" with "1" argument(s): "The method or operation is not implemented."
At line:26 char:2
+     $x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : NotImplementedException
 
Press any Key to continue with folder: 2
Exception calling "ReadKey" with "1" argument(s): "The method or operation is not implemented."
At line:26 char:2
+     $x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : NotImplementedException
0
Sukpal JohalAuthor Commented:
ITguy565 yours actually worked in launching files from the subfolders but did not launch from the main directory.  Alos is there a chance that instead of having the "PAUSE" there is a pop up box?
0
Thomas UCommented:
Where do you try to run that script. I mean Windows Version ? Powershell Version?

If only the ReadKey is the problem, comment those two lines out (write a # before the lines 25 and 26) instead create a line and write pause
0
Sukpal JohalAuthor Commented:
Thomas U. I like your script but with a slight issue. The Main directory files are launched alongside the 1st subfolder, from there the prompt works and only the 2nd subfolders files are launched. Any chance of getting the main directory to launch first and then navigate through the sub directories.

$workingdir = "C:\Users\Administrator\Downloads\test"

set-location -path $workingDir


#execute all files within the workingdir, no recurse
$rootfolder = Get-ChildItem -file #| % { $_.FullName }
foreach ($file in $rootfolder){
      start $file
}

# get all folders in $workingdir
$folderNames = Get-ChildItem -Recurse | ?{ $_.PSIsContainer }

#loop through folders and execute, pause after folder
foreach ($folder in $foldernames){
      #get all files in the subfolder with fullpath
      $files = get-ChildItem -Path $folder -File | % { $_.FullName }
            # need loop through all files
            foreach ($fileSub in $files){
          
            start $fileSub
            }
      # Waits for user to continue
      Write-Host "Press any Key to continue with folder:" $folder
      #$x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
    Read-Host -Prompt "Press any key to continue"
      
}
0
Sukpal JohalAuthor Commented:
I have it.

$workingdir = "C:\Users\Administrator\Downloads\test"

set-location -path $workingDir


#execute all files within the workingdir, no recurse
$rootfolder = Get-ChildItem -file #| % { $_.FullName }
foreach ($file in $rootfolder){
      start $file
}

Read-Host -Prompt "Press any key to continue"

# get all folders in $workingdir
$folderNames = Get-ChildItem -Recurse | ?{ $_.PSIsContainer }

#loop through folders and execute, pause after folder
foreach ($folder in $foldernames){
      #get all files in the subfolder with fullpath
      $files = get-ChildItem -Path $folder -File | % { $_.FullName }
            # need loop through all files
            foreach ($fileSub in $files){
          
            start $fileSub
            }
      # Waits for user to continue
      Write-Host "Press any Key to continue with folder:" $folder
      #$x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
    Read-Host -Prompt "Press any key to continue"
      
}
0
Thomas UCommented:
wonderful ;) nice to hear that it worked.
0
Sukpal JohalAuthor Commented:
Thank you guys for your help
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.