wullxz
wullxz

Reputation: 19540

Script a new powershell window with powershell?

I would like to automatically launch a powershell and run a few commands from another powershell (I want to start it from another powershell because of the advantages of functions).
I would like to do so because I'm testing a *.dll coded in C# with powershell and I have to constantly launch and quit powershell because it's not possible to unload a dll in powershell and therefore it's not possible to reload the library with its classes.

Is there a way to automate powershell just like the com-object automation with office?

Upvotes: 1

Views: 2871

Answers (2)

Joey
Joey

Reputation: 354794

What about just using a script or script blocks?

powershell {
   Add-Type foo.dll
   [Foo]::DoSomething();
}

should work.

If you need interactivity and need to try multiple commands at will, you could use

powershell -noexit { Add-Type foo.dll }

In that case it can be useful to at least change the prompt color so you know whether you're in the test sub-shell or in the parent one:

function Test-DLL {
  powershell -noexit {
    function prompt {
      Write-Host -n -fore yellow "Test $PWD>"
      ' '
    }

    Add-Type foo.dll
  }
}

I tend to define a small function in the following way too:

"function $([char]4) { exit }" | Invoke-Expression

which allows me to close PowerShell with Ctrl+D, Enter (half an analog to Unix shells).

Upvotes: 3

rlegendi
rlegendi

Reputation: 10606

Perhaps not the most elegant solution but should do it (I'm not a PS pro):

cmd /c start powershell

Upvotes: 1

Related Questions