7

I have a series of ps1 files in a directory, all of which have different names. I am trying to run them one after another with Start-Process and the -Wait parameter. How does loop through all the files in a directory and run one PowerShell script after another? There are no subfolders, and no files that are not of type ps1.

Here is my start:

$DirectoryList = Get-Content -Path C:\test foreach ($Directory in $DirectoryList) { Start-Process -FilePath powershell.exe -ArgumentList ('"{0}\How To Read Me.ps1" -Path "{1}"' -f $PSScriptRoot, $Directory); } 

1 Answer 1

29

You could simply use the call operator (&):

Get-ChildItem 'C:\test' | ForEach-Object { & $_.FullName } 
Sign up to request clarification or add additional context in comments.

4 Comments

Ansgars answer does the trick but I just wanna point out (obvious to some but certainly not all) that it's quite frickin' important to have full control of the path and everything in it before looping through and executing every single file within so no harmful code is being executed.
Get-ChildItem | % { & $_.FullName } That will run everything in your current directory.
Here's even shorter :) gci | %{$_.FullName}
@DevinStokes The ampersand is not optional. And the shortest possible version (without defining custom functions/aliases) is ls|%{&$_.FullName}.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.